Skip to content

Commit c81522f

Browse files
tanhauhauivanhoferdummdidummignatiusmbbenmccann
committed
breaking: conditional ActionReturn type if Parameter is void (#7442)
--------- Co-authored-by: Ivan Hofer <[email protected]> Co-authored-by: Simon H <[email protected]> Co-authored-by: Ignatius Bagus <[email protected]> Co-authored-by: Ben McCann <[email protected]> Co-authored-by: Simon Holthausen <[email protected]>
1 parent 8e51b51 commit c81522f

File tree

4 files changed

+170
-6
lines changed

4 files changed

+170
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* **breaking** Minimum supported Node version is now Node 14
66
* **breaking** Minimum supported TypeScript version is now 5 (it will likely work with lower versions, but we make no guarantess about that)
77
* **breaking** Stricter types for `createEventDispatcher` (see PR for migration instructions) ([#7224](https://github.com/sveltejs/svelte/pull/7224))
8+
* **breaking** Stricter types for `Action` and `ActionReturn` (see PR for migration instructions) ([#7224](https://github.com/sveltejs/svelte/pull/7224))
89

910
## Unreleased (3.0)
1011

src/runtime/action/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* Actions can return an object containing the two properties defined in this interface. Both are optional.
33
* - update: An action can have a parameter. This method will be called whenever that parameter changes,
4-
* immediately after Svelte has applied updates to the markup.
4+
* immediately after Svelte has applied updates to the markup. `ActionReturn` and `ActionReturn<never>` both
5+
* mean that the action accepts no parameters, which makes it illegal to set the `update` method.
56
* - destroy: Method that is called after the element is unmounted
67
*
78
* Additionally, you can specify which additional attributes and events the action enables on the applied element.
@@ -25,8 +26,8 @@
2526
*
2627
* Docs: https://svelte.dev/docs#template-syntax-element-directives-use-action
2728
*/
28-
export interface ActionReturn<Parameter = any, Attributes extends Record<string, any> = Record<never, any>> {
29-
update?: (parameter: Parameter) => void;
29+
export interface ActionReturn<Parameter = never, Attributes extends Record<string, any> = Record<never, any>> {
30+
update?: [Parameter] extends [never] ? never : (parameter: Parameter) => void;
3031
destroy?: () => void;
3132
/**
3233
* ### DO NOT USE THIS
@@ -42,15 +43,21 @@ export interface ActionReturn<Parameter = any, Attributes extends Record<string,
4243
* The following example defines an action that only works on `<div>` elements
4344
* and optionally accepts a parameter which it has a default value for:
4445
* ```ts
45-
* export const myAction: Action<HTMLDivElement, { someProperty: boolean }> = (node, param = { someProperty: true }) => {
46+
* export const myAction: Action<HTMLDivElement, { someProperty: boolean } | undefined> = (node, param = { someProperty: true }) => {
4647
* // ...
4748
* }
4849
* ```
50+
* `Action<HTMLDivElement>` and `Action<HTMLDiveElement, never>` both signal that the action accepts no parameters.
51+
*
4952
* You can return an object with methods `update` and `destroy` from the function and type which additional attributes and events it has.
5053
* See interface `ActionReturn` for more details.
5154
*
5255
* Docs: https://svelte.dev/docs#template-syntax-element-directives-use-action
5356
*/
54-
export interface Action<Element = HTMLElement, Parameter = any, Attributes extends Record<string, any> = Record<never, any>> {
55-
<Node extends Element>(node: Node, parameter?: Parameter): void | ActionReturn<Parameter, Attributes>;
57+
export interface Action<Element = HTMLElement, Parameter = never, Attributes extends Record<string, any> = Record<never, any>> {
58+
<Node extends Element>(...args: [Parameter] extends [never] ? [node: Node] : undefined extends Parameter ? [node: Node, parameter?: Parameter] : [node: Node, parameter: Parameter]): void | ActionReturn<Parameter, Attributes>;
5659
}
60+
61+
// Implementation notes:
62+
// - undefined extends X instead of X extends undefined makes this work better with both strict and nonstrict mode
63+
// - [X] extends [never] is needed, X extends never would reduce the whole resulting type to never and not to one of the condition outcomes

src/runtime/internal/lifecycle.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ export function onDestroy(fn: () => any) {
5757
}
5858

5959
export interface EventDispatcher<EventMap extends Record<string, any>> {
60+
// Implementation notes:
61+
// - undefined extends X instead of X extends undefined makes this work better with both strict and nonstrict mode
62+
// - [X] extends [never] is needed, X extends never would reduce the whole resulting type to never and not to one of the condition outcomes
6063
<Type extends keyof EventMap>(
6164
...args: [EventMap[Type]] extends [never] ? [type: Type, parameter?: null | undefined, options?: DispatchOptions] :
6265
null extends EventMap[Type] ? [type: Type, parameter?: EventMap[Type], options?: DispatchOptions] :

test/types/actions.ts

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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

Comments
 (0)