Skip to content

Commit 00e8ebd

Browse files
authored
fix: widen ComponentProps constraint to accept more component shapes (#12666)
language tools has to type its own shape for backwards compatibility, and it currently doesn't include the `$on` and `$set` methods, which means without widening the type as done here you would get a "this shape is not accepted" type error when passing it to `ComponentProps` closes #12627
1 parent ee1a3df commit 00e8ebd

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

.changeset/warm-cycles-call.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: widen `ComponentProps` constraint to accept more component shapes

packages/svelte/src/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ export type ComponentEvents<Comp extends SvelteComponent> =
226226
* withProps(MyComponent, { foo: 'bar' });
227227
* ```
228228
*/
229-
export type ComponentProps<Comp extends SvelteComponent | Component<any>> =
229+
export type ComponentProps<Comp extends SvelteComponent | Component<any, any>> =
230230
Comp extends SvelteComponent<infer Props>
231231
? Props
232-
: Comp extends Component<infer Props>
232+
: Comp extends Component<infer Props, any>
233233
? Props
234234
: never;
235235

packages/svelte/tests/types/component.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ const functionComponentProps: ComponentProps<typeof functionComponent> = {
261261
prop: 1
262262
};
263263

264+
// Test that self-typed functions are correctly inferred, too (use case: language tools has its own shape for backwards compatibility)
265+
const functionComponentProps2: ComponentProps<(a: any, b: { a: true }) => { foo: string }> = {
266+
a: true
267+
};
268+
264269
mount(functionComponent, {
265270
target: null as any as Document | Element | ShadowRoot,
266271
props: {

packages/svelte/types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ declare module 'svelte' {
223223
* withProps(MyComponent, { foo: 'bar' });
224224
* ```
225225
*/
226-
export type ComponentProps<Comp extends SvelteComponent | Component<any>> =
226+
export type ComponentProps<Comp extends SvelteComponent | Component<any, any>> =
227227
Comp extends SvelteComponent<infer Props>
228228
? Props
229-
: Comp extends Component<infer Props>
229+
: Comp extends Component<infer Props, any>
230230
? Props
231231
: never;
232232

0 commit comments

Comments
 (0)