Skip to content

Commit

Permalink
fix: fall back to any instead of unknown for untyped $props (#2582
Browse files Browse the repository at this point in the history
)

The idea was to be conservative about it, but this causes problems for people not wanting strict type checking but only a basic form of it. Falling back to `any` is the more pragmatic choice

#2556
  • Loading branch information
dummdidumm authored Nov 12, 2024
1 parent 437b0e1 commit 77160ff
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 14 deletions.
17 changes: 8 additions & 9 deletions packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export class ExportedNames {
? element.initializer.arguments[0]
: element.initializer;
const type = !initializer
? 'unknown'
? 'any'
: ts.isAsExpression(initializer)
? initializer.type.getText()
: ts.isStringLiteral(initializer)
Expand All @@ -300,13 +300,13 @@ export class ExportedNames {
: ts.isArrowFunction(initializer)
? 'Function'
: ts.isObjectLiteralExpression(initializer)
? 'Record<string, unknown>'
? 'Record<string, any>'
: ts.isArrayLiteralExpression(initializer)
? 'unknown[]'
: 'unknown';
? 'any[]'
: 'any';
props.push(`${name}?: ${type}`);
} else {
props.push(`${name}: unknown`);
props.push(`${name}: any`);
}
}
}
Expand All @@ -317,15 +317,14 @@ export class ExportedNames {

if (props.length > 0) {
propsStr =
`{ ${props.join(', ')} }` +
(withUnknown ? ' & Record<string, unknown>' : '');
`{ ${props.join(', ')} }` + (withUnknown ? ' & Record<string, any>' : '');
} else if (withUnknown) {
propsStr = 'Record<string, unknown>';
propsStr = 'Record<string, any>';
} else {
propsStr = 'Record<string, never>';
}
} else {
propsStr = 'Record<string, unknown>';
propsStr = 'Record<string, any>';
}

// Create a virtual type alias for the unnamed generic and reuse it for the props return type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///<reference types="svelte" />
;function render() {

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();
let/** @typedef {{ a: any, b?: boolean, c?: number, d?: string, e?: any, f?: Record<string, any>, g?: typeof foo, h?: any[], i?: any, j?: any, 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();
;
async () => {};
return { props: /** @type {$$ComponentProps} */({}), exports: {}, bindings: __sveltets_$$bindings('j', 'k'), slots: {}, events: {} }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///<reference types="svelte" />
;function render() {

let/** @typedef {{ a: unknown, b?: unknown }} $$ComponentProps *//** @type {$$ComponentProps} */ { a, b = $bindable() } = $props();
let/** @typedef {{ a: any, b?: any }} $$ComponentProps *//** @type {$$ComponentProps} */ { a, b = $bindable() } = $props();
;
async () => {};
return { props: /** @type {$$ComponentProps} */({}), exports: {}, bindings: __sveltets_$$bindings('b'), slots: {}, events: {} }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///<reference types="svelte" />
;function render() {

let/** @typedef {{ props: unknown }} $$ComponentProps *//** @type {$$ComponentProps} */ { props } = $props();
let/** @typedef {{ props: any }} $$ComponentProps *//** @type {$$ComponentProps} */ { props } = $props();
let state = $state(0);
let derived = $derived(state * 2);
;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///<reference types="svelte" />
;function render() {
/*Ω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Ω*/
/*Ωignore_startΩ*/;type $$ComponentProps = { a: any, b?: boolean, c?: number, d?: string, e?: any, f?: Record<string, any>, g?: typeof foo, h?: Bar, i?: Baz, j?: any[], k?: any, l?: any, m?: number, n?: Function };/*Ωignore_endΩ*/
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();
;
async () => {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///<reference types="svelte" />
;function render() {
/*Ωignore_startΩ*/;type $$ComponentProps = { a: unknown, b?: unknown, c?: number };/*Ωignore_endΩ*/
/*Ωignore_startΩ*/;type $$ComponentProps = { a: any, b?: any, c?: number };/*Ωignore_endΩ*/
let { a, b = $bindable(), c = $bindable(0) as number }: $$ComponentProps = $props();
;
async () => {};
Expand Down

0 comments on commit 77160ff

Please sign in to comment.