Skip to content

Commit b619870

Browse files
authored
(fix) remove overzealous $$Props check (#1920)
The check if exports are assignable to $$Props is removed because a component should be allowed to use less props than defined (it just ignores them)
1 parent 70749bb commit b619870

File tree

5 files changed

+7
-20
lines changed

5 files changed

+7
-20
lines changed

packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/$$props-invalid3/expectedv2.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,5 @@
66
"message": "Argument of type '$$Props' is not assignable to parameter of type '{ wrong: boolean; }'.\n Property 'wrong' is missing in type '$$Props' but required in type '{ wrong: boolean; }'.",
77
"code": 2345,
88
"tags": []
9-
},
10-
{
11-
"range": { "start": { "line": 1, "character": 11 }, "end": { "line": 1, "character": 18 } },
12-
"severity": 1,
13-
"source": "ts",
14-
"message": "Argument of type '{ wrong: boolean; }' is not assignable to parameter of type '$$Props'.\n Object literal may only specify known properties, and 'wrong' does not exist in type '$$Props'.",
15-
"code": 2345,
16-
"tags": []
179
}
1810
]

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -353,21 +353,16 @@ export class ExportedNames {
353353
if (this.uses$$Props) {
354354
const lets = names.filter(([, { isLet }]) => isLet);
355355
const others = names.filter(([, { isLet }]) => !isLet);
356-
// We need to check both ways:
357-
// - The check if exports are assignable to $$Props is necessary to make sure
358-
// no props are missing. Use Partial<$$Props> in case $$props is used.
359356
// - The check if $$Props is assignable to exports is necessary to make sure no extraneous props
360357
// are defined and that no props are required that should be optional
361-
// __sveltets_2_ensureRightProps needs to be declared in a way that doesn't affect the type result of props
358+
// - The check if exports are assignable to $$Props is not done because a component should be allowed
359+
// to use less props than defined (it just ignores them)
360+
// - __sveltets_2_ensureRightProps needs to be declared in a way that doesn't affect the type result of props
362361
return (
363362
'{...__sveltets_2_ensureRightProps<{' +
364363
this.createReturnElementsType(lets).join(',') +
365364
'}>(__sveltets_2_any("") as $$Props), ' +
366-
'...__sveltets_2_ensureRightProps<' +
367-
(uses$$propsOr$$restProps ? 'Partial<$$Props>' : '$$Props') +
368-
'>({' +
369-
this.createReturnElements(lets, false).join(',') +
370-
'}), ...{} as unknown as $$Props, ...{' +
365+
'...{} as unknown as $$Props, ...{' +
371366
// We add other exports of classes and functions here because
372367
// they need to appear in the props object in order to properly
373368
// type bind:xx but they are not needed to be part of $$Props

packages/svelte2tsx/test/svelte2tsx/samples/ts-$$Props-interface/expectedv2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
;
3333
async () => {};
34-
return { props: {...__sveltets_2_ensureRightProps<{exported1: string,exported2?: string,name1?: string,name2: string,renamed1?: string,renamed2: string}>(__sveltets_2_any("") as $$Props), ...__sveltets_2_ensureRightProps<$$Props>({exported1: exported1,exported2: exported2,name1: name1,name2: name2,renamed1: rename1,renamed2: rename2}), ...{} as unknown as $$Props, ...{Foo: Foo, bar: bar, baz: baz, RenamedFoo: RenameFoo, renamedbar: renamebar, renamedbaz: renamebaz} as {Foo?: typeof Foo,bar?: typeof bar,baz?: string,RenamedFoo?: typeof RenameFoo,renamedbar?: typeof renamebar,renamedbaz?: string}}, slots: {}, events: {} }}
34+
return { props: {...__sveltets_2_ensureRightProps<{exported1: string,exported2?: string,name1?: string,name2: string,renamed1?: string,renamed2: string}>(__sveltets_2_any("") as $$Props), ...{} as unknown as $$Props, ...{Foo: Foo, bar: bar, baz: baz, RenamedFoo: RenameFoo, renamedbar: renamebar, renamedbaz: renamebaz} as {Foo?: typeof Foo,bar?: typeof bar,baz?: string,RenamedFoo?: typeof RenameFoo,renamedbar?: typeof renamebar,renamedbaz?: string}}, slots: {}, events: {} }}
3535

3636
export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_with_any_event(render())) {
3737
}

packages/svelte2tsx/test/svelte2tsx/samples/ts-$$Props-type/expectedv2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
;
3333
async () => {};
34-
return { props: {...__sveltets_2_ensureRightProps<{exported1: string,exported2?: string,name1?: string,name2: string,renamed1?: string,renamed2: string}>(__sveltets_2_any("") as $$Props), ...__sveltets_2_ensureRightProps<$$Props>({exported1: exported1,exported2: exported2,name1: name1,name2: name2,renamed1: rename1,renamed2: rename2}), ...{} as unknown as $$Props, ...{Foo: Foo, bar: bar, baz: baz, RenamedFoo: RenameFoo, renamedbar: renamebar, renamedbaz: renamebaz} as {Foo?: typeof Foo,bar?: typeof bar,baz?: string,RenamedFoo?: typeof RenameFoo,renamedbar?: typeof renamebar,renamedbaz?: string}}, slots: {}, events: {} }}
34+
return { props: {...__sveltets_2_ensureRightProps<{exported1: string,exported2?: string,name1?: string,name2: string,renamed1?: string,renamed2: string}>(__sveltets_2_any("") as $$Props), ...{} as unknown as $$Props, ...{Foo: Foo, bar: bar, baz: baz, RenamedFoo: RenameFoo, renamedbar: renamebar, renamedbaz: renamebaz} as {Foo?: typeof Foo,bar?: typeof bar,baz?: string,RenamedFoo?: typeof RenameFoo,renamedbar?: typeof renamebar,renamedbaz?: string}}, slots: {}, events: {} }}
3535

3636
export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_with_any_event(render())) {
3737
}

packages/svelte2tsx/test/svelte2tsx/samples/ts-$$Props-with-$$props/expectedv2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
async () => {
1414

1515
$$props;};
16-
return { props: {...__sveltets_2_ensureRightProps<{}>(__sveltets_2_any("") as $$Props), ...__sveltets_2_ensureRightProps<Partial<$$Props>>({}), ...{} as unknown as $$Props, ...{c: c} as {c?: typeof c}}, slots: {}, events: {} }}
16+
return { props: {...__sveltets_2_ensureRightProps<{}>(__sveltets_2_any("") as $$Props), ...{} as unknown as $$Props, ...{c: c} as {c?: typeof c}}, slots: {}, events: {} }}
1717

1818
export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_with_any_event(render())) {
1919
get c() { return __sveltets_2_nonNullable(this.$$prop_def.c) }

0 commit comments

Comments
 (0)