diff --git a/CHANGELOG.md b/CHANGELOG.md index a6345d99b3..e28643f1f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * **breaking** Minimum supported Node version is now Node 14 * **breaking** Minimum supported TypeScript version is now 5 (it will likely work with lower versions, but we make no guarantess about that) * **breaking** Stricter types for `createEventDispatcher` (see PR for migration instructions) ([#7224](https://github.com/sveltejs/svelte/pull/7224)) +* **breaking** Stricter types for `Action` and `ActionReturn` (see PR for migration instructions) ([#7224](https://github.com/sveltejs/svelte/pull/7224)) ## Unreleased (3.0) diff --git a/src/runtime/internal/lifecycle.ts b/src/runtime/internal/lifecycle.ts index 7592e72a38..29888e9de3 100644 --- a/src/runtime/internal/lifecycle.ts +++ b/src/runtime/internal/lifecycle.ts @@ -57,6 +57,9 @@ export function onDestroy(fn: () => any) { } export interface EventDispatcher> { + // Implementation notes: + // - undefined extends X instead of X extends undefined makes this work better with both strict and nonstrict mode + // - [X] extends [never] is needed, X extends never would reduce the whole resulting type to never and not to one of the condition outcomes ( ...args: [EventMap[Type]] extends [never] ? [type: Type, parameter?: null | undefined, options?: DispatchOptions] : null extends EventMap[Type] ? [type: Type, parameter?: EventMap[Type], options?: DispatchOptions] : diff --git a/test/types/actions.ts b/test/types/actions.ts new file mode 100644 index 0000000000..d139143f12 --- /dev/null +++ b/test/types/actions.ts @@ -0,0 +1,148 @@ +import type { Action, ActionReturn } from '$runtime/action'; + +// ---------------- Action + +const href: Action = (node) => { + node.href = ''; + // @ts-expect-error + node.href = 1; +}; +href; + +const required: Action = (node, param) => { + node; + param; +}; +required(null as any, true); +// @ts-expect-error (only in strict mode) boolean missing +required(null as any); +// @ts-expect-error no boolean +required(null as any, 'string'); + +const required1: Action = (node, param) => { + node; + param; + return { + update: (p) => p === true, + destroy: () => {} + } +}; +required1; + +const required2: Action = (node) => { + node; +}; +required2; + +const required3: Action = (node, param) => { + node; + param; + return { + // @ts-expect-error comparison always resolves to false + update: (p) => p === 'd', + destroy: () => {} + } +}; +required3; + +const optional: Action = (node, param?) => { + node; + param; +}; +optional(null as any, true); +optional(null as any); +// @ts-expect-error no boolean +optional(null as any, 'string'); + +const optional1: Action = (node, param?) => { + node; + param; + return { + update: (p) => p === true, + destroy: () => {} + } +}; +optional1; + +const optional2: Action = (node) => { + node; +}; +optional2; + +const optional3: Action = (node, param) => { + node; + param; +}; +optional3; + +const optional4: Action = (node, param?) => { + node; + param; + return { + // @ts-expect-error comparison always resolves to false + update: (p) => p === 'd', + destroy: () => {} + } +}; +optional4; + +const no: Action = (node) => { + node; +}; +// @ts-expect-error second param +no(null as any, true); +no(null as any); +// @ts-expect-error second param +no(null as any, 'string'); + +const no1: Action = (node) => { + node; + return { + destroy: () => {} + } +}; +no1; + +// @ts-expect-error param given +const no2: Action = (node, param?) => {} + +// @ts-expect-error param given +const no3: Action = (node, param) => {} + +// @ts-expect-error update method given +const no4: Action = (node) => { + return { + update: () => {}, + destroy: () => {} + } +} + +// ---------------- ActionReturn + +const requiredReturn: ActionReturn = { + update: (p) => p.toString(), +}; +requiredReturn; + +const optionalReturn: ActionReturn = { + update: (p) => { + p === true; + // @ts-expect-error could be undefined + p.toString(); + }, +}; +optionalReturn; + +const invalidProperty: ActionReturn = { + // @ts-expect-error invalid property + invalid: () => {}, +}; +invalidProperty; + +type Attributes = ActionReturn['$$_attributes']; +const attributes: Attributes = { a: 'a' }; +attributes; +// @ts-expect-error wrong type +const invalidAttributes1: Attributes = { a: 1 }; +// @ts-expect-error missing prop +const invalidAttributes2: Attributes = {};