Skip to content

Commit 0698bc7

Browse files
committed
fix: infer types for $bindable, infer function type from arrow function
#2577
1 parent 5bd4663 commit 0698bc7

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -277,22 +277,33 @@ export class ExportedNames {
277277
props.push(`form: import('./$types.js').ActionData`);
278278
}
279279
} else if (element.initializer) {
280-
const type = ts.isAsExpression(element.initializer)
281-
? element.initializer.type.getText()
282-
: ts.isStringLiteral(element.initializer)
283-
? 'string'
284-
: ts.isNumericLiteral(element.initializer)
285-
? 'number'
286-
: element.initializer.kind === ts.SyntaxKind.TrueKeyword ||
287-
element.initializer.kind === ts.SyntaxKind.FalseKeyword
288-
? 'boolean'
289-
: ts.isIdentifier(element.initializer)
290-
? `typeof ${element.initializer.text}`
291-
: ts.isObjectLiteralExpression(element.initializer)
292-
? 'Record<string, unknown>'
293-
: ts.isArrayLiteralExpression(element.initializer)
294-
? 'unknown[]'
295-
: 'unknown';
280+
const initializer =
281+
ts.isCallExpression(element.initializer) &&
282+
ts.isIdentifier(element.initializer.expression) &&
283+
element.initializer.expression.text === '$bindable'
284+
? element.initializer.arguments[0]
285+
: element.initializer;
286+
const type = !initializer
287+
? 'unknown'
288+
: ts.isAsExpression(initializer)
289+
? initializer.type.getText()
290+
: ts.isStringLiteral(initializer)
291+
? 'string'
292+
: ts.isNumericLiteral(initializer)
293+
? 'number'
294+
: initializer.kind === ts.SyntaxKind.TrueKeyword ||
295+
initializer.kind === ts.SyntaxKind.FalseKeyword
296+
? 'boolean'
297+
: ts.isIdentifier(initializer) &&
298+
initializer.text !== 'undefined'
299+
? `typeof ${initializer.text}`
300+
: ts.isArrowFunction(initializer)
301+
? 'Function'
302+
: ts.isObjectLiteralExpression(initializer)
303+
? 'Record<string, unknown>'
304+
: ts.isArrayLiteralExpression(initializer)
305+
? 'unknown[]'
306+
: 'unknown';
296307
props.push(`${name}?: ${type}`);
297308
} else {
298309
props.push(`${name}: unknown`);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
///<reference types="svelte" />
22
;function render() {
33

4-
let/** @typedef {{ a: unknown, b?: boolean, c?: number, d?: string, e?: unknown, f?: Record<string, unknown>, g?: typeof foo, h?: unknown[] }} $$ComponentProps *//** @type {$$ComponentProps} */ { a, b = true, c = 1, d = '', e = null, f = {}, g = foo, h = [] } = $props();
4+
let/** @typedef {{ a: unknown, b?: boolean, c?: number, d?: string, e?: unknown, f?: Record<string, unknown>, g?: typeof foo, h?: unknown[], i?: unknown, j?: unknown, k?: number, l?: Function }} $$ComponentProps *//** @type {$$ComponentProps} */ { a, b = true, c = 1, d = '', e = null, f = {}, g = foo, h = [], i = undefined, j = $bindable(), k = $bindable(1), l = () => {} } = $props();
55
;
66
async () => {};
7-
return { props: /** @type {$$ComponentProps} */({}), exports: {}, bindings: __sveltets_$$bindings(''), slots: {}, events: {} }}
7+
return { props: /** @type {$$ComponentProps} */({}), exports: {}, bindings: __sveltets_$$bindings('j', 'k'), slots: {}, events: {} }}
88
const Input__SvelteComponent_ = __sveltets_2_fn_component(render());
99
type Input__SvelteComponent_ = ReturnType<typeof Input__SvelteComponent_>;
1010
export default Input__SvelteComponent_;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<script>
2-
let { a, b = true, c = 1, d = '', e = null, f = {}, g = foo, h = [] } = $props();
2+
let { a, b = true, c = 1, d = '', e = null, f = {}, g = foo, h = [], i = undefined, j = $bindable(), k = $bindable(1), l = () => {} } = $props();
33
</script>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
///<reference types="svelte" />
22
;function render() {
3-
/*Ωignore_startΩ*/;type $$ComponentProps = { a: unknown, b?: boolean, c?: number, d?: string, e?: unknown, f?: Record<string, unknown>, g?: typeof foo, h?: Bar, i?: Baz, j?: unknown[] };/*Ωignore_endΩ*/
4-
let { a, b = true, c = 1, d = '', e = null, f = {}, g = foo, h = null as Bar, i = null as any as Baz, j = [] }: $$ComponentProps = $props();
3+
/*Ωignore_startΩ*/;type $$ComponentProps = { a: unknown, b?: boolean, c?: number, d?: string, e?: unknown, f?: Record<string, unknown>, g?: typeof foo, h?: Bar, i?: Baz, j?: unknown[], k?: unknown, l?: unknown, m?: number, n?: Function };/*Ωignore_endΩ*/
4+
let { a, b = true, c = 1, d = '', e = null, f = {}, g = foo, h = null as Bar, i = null as any as Baz, j = [], k = undefined, l = $bindable(), m = $bindable(1), n = () => {} }: $$ComponentProps = $props();
55
;
66
async () => {};
7-
return { props: {} as any as $$ComponentProps, exports: {}, bindings: __sveltets_$$bindings(''), slots: {}, events: {} }}
7+
return { props: {} as any as $$ComponentProps, exports: {}, bindings: __sveltets_$$bindings('l', 'm'), slots: {}, events: {} }}
88
const Input__SvelteComponent_ = __sveltets_2_fn_component(render());
99
type Input__SvelteComponent_ = ReturnType<typeof Input__SvelteComponent_>;
1010
export default Input__SvelteComponent_;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<script>
2-
let { a, b = true, c = 1, d = '', e = null, f = {}, g = foo, h = null as Bar, i = null as any as Baz, j = [] } = $props();
2+
let { a, b = true, c = 1, d = '', e = null, f = {}, g = foo, h = null as Bar, i = null as any as Baz, j = [], k = undefined, l = $bindable(), m = $bindable(1), n = () => {} } = $props();
33
</script>

0 commit comments

Comments
 (0)