|
| 1 | +import type { Action, ActionReturn } from '$runtime/action'; |
| 2 | + |
| 3 | +// ---------------- Action |
| 4 | + |
| 5 | +const href: Action<HTMLAnchorElement> = (node) => { |
| 6 | + node.href = ''; |
| 7 | + // @ts-expect-error |
| 8 | + node.href = 1; |
| 9 | +}; |
| 10 | +href; |
| 11 | + |
| 12 | +const required: Action<HTMLElement, boolean> = (node, param) => { |
| 13 | + node; |
| 14 | + param; |
| 15 | +}; |
| 16 | +required(null as any, true); |
| 17 | +// @ts-expect-error (only in strict mode) boolean missing |
| 18 | +required(null as any); |
| 19 | +// @ts-expect-error no boolean |
| 20 | +required(null as any, 'string'); |
| 21 | + |
| 22 | +const required1: Action<HTMLElement, boolean> = (node, param) => { |
| 23 | + node; |
| 24 | + param; |
| 25 | + return { |
| 26 | + update: (p) => p === true, |
| 27 | + destroy: () => {} |
| 28 | + }; |
| 29 | +}; |
| 30 | +required1; |
| 31 | + |
| 32 | +const required2: Action<HTMLElement, boolean> = (node) => { |
| 33 | + node; |
| 34 | +}; |
| 35 | +required2; |
| 36 | + |
| 37 | +const required3: Action<HTMLElement, boolean> = (node, param) => { |
| 38 | + node; |
| 39 | + param; |
| 40 | + return { |
| 41 | + // @ts-expect-error comparison always resolves to false |
| 42 | + update: (p) => p === 'd', |
| 43 | + destroy: () => {} |
| 44 | + }; |
| 45 | +}; |
| 46 | +required3; |
| 47 | + |
| 48 | +const optional: Action<HTMLElement, boolean | undefined> = (node, param?) => { |
| 49 | + node; |
| 50 | + param; |
| 51 | +}; |
| 52 | +optional(null as any, true); |
| 53 | +optional(null as any); |
| 54 | +// @ts-expect-error no boolean |
| 55 | +optional(null as any, 'string'); |
| 56 | + |
| 57 | +const optional1: Action<HTMLElement, boolean | undefined> = (node, param?) => { |
| 58 | + node; |
| 59 | + param; |
| 60 | + return { |
| 61 | + update: (p) => p === true, |
| 62 | + destroy: () => {} |
| 63 | + }; |
| 64 | +}; |
| 65 | +optional1; |
| 66 | + |
| 67 | +const optional2: Action<HTMLElement, boolean | undefined> = (node) => { |
| 68 | + node; |
| 69 | +}; |
| 70 | +optional2; |
| 71 | + |
| 72 | +const optional3: Action<HTMLElement, boolean | undefined> = (node, param) => { |
| 73 | + node; |
| 74 | + param; |
| 75 | +}; |
| 76 | +optional3; |
| 77 | + |
| 78 | +const optional4: Action<HTMLElement, boolean | undefined> = (node, param?) => { |
| 79 | + node; |
| 80 | + param; |
| 81 | + return { |
| 82 | + // @ts-expect-error comparison always resolves to false |
| 83 | + update: (p) => p === 'd', |
| 84 | + destroy: () => {} |
| 85 | + }; |
| 86 | +}; |
| 87 | +optional4; |
| 88 | + |
| 89 | +const no: Action<HTMLElement, never> = (node) => { |
| 90 | + node; |
| 91 | +}; |
| 92 | +// @ts-expect-error second param |
| 93 | +no(null as any, true); |
| 94 | +no(null as any); |
| 95 | +// @ts-expect-error second param |
| 96 | +no(null as any, 'string'); |
| 97 | + |
| 98 | +const no1: Action<HTMLElement, never> = (node) => { |
| 99 | + node; |
| 100 | + return { |
| 101 | + destroy: () => {} |
| 102 | + }; |
| 103 | +}; |
| 104 | +no1; |
| 105 | + |
| 106 | +// @ts-expect-error param given |
| 107 | +const no2: Action<HTMLElement, never> = (node, param?) => {}; |
| 108 | +no2; |
| 109 | + |
| 110 | +// @ts-expect-error param given |
| 111 | +const no3: Action<HTMLElement, never> = (node, param) => {}; |
| 112 | +no3; |
| 113 | + |
| 114 | +// @ts-expect-error update method given |
| 115 | +const no4: Action<HTMLElement, never> = (node) => { |
| 116 | + return { |
| 117 | + update: () => {}, |
| 118 | + destroy: () => {} |
| 119 | + }; |
| 120 | +}; |
| 121 | +no4; |
| 122 | + |
| 123 | +// ---------------- ActionReturn |
| 124 | + |
| 125 | +const requiredReturn: ActionReturn<string> = { |
| 126 | + update: (p) => p.toString() |
| 127 | +}; |
| 128 | +requiredReturn; |
| 129 | + |
| 130 | +const optionalReturn: ActionReturn<boolean | undefined> = { |
| 131 | + update: (p) => { |
| 132 | + p === true; |
| 133 | + // @ts-expect-error could be undefined |
| 134 | + p.toString(); |
| 135 | + } |
| 136 | +}; |
| 137 | +optionalReturn; |
| 138 | + |
| 139 | +const invalidProperty: ActionReturn = { |
| 140 | + // @ts-expect-error invalid property |
| 141 | + invalid: () => {} |
| 142 | +}; |
| 143 | +invalidProperty; |
| 144 | + |
| 145 | +type Attributes = ActionReturn<never, { a: string; }>['$$_attributes']; |
| 146 | +const attributes: Attributes = { a: 'a' }; |
| 147 | +attributes; |
| 148 | +// @ts-expect-error wrong type |
| 149 | +const invalidAttributes1: Attributes = { a: 1 }; |
| 150 | +invalidAttributes1; |
| 151 | +// @ts-expect-error missing prop |
| 152 | +const invalidAttributes2: Attributes = {}; |
| 153 | +invalidAttributes2; |
0 commit comments