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
pull/12667/head
Simon H 5 months ago committed by GitHub
parent ee1a3df835
commit 00e8ebde1d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: widen `ComponentProps` constraint to accept more component shapes

@ -226,10 +226,10 @@ export type ComponentEvents<Comp extends SvelteComponent> =
* withProps(MyComponent, { foo: 'bar' }); * withProps(MyComponent, { foo: 'bar' });
* ``` * ```
*/ */
export type ComponentProps<Comp extends SvelteComponent | Component<any>> = export type ComponentProps<Comp extends SvelteComponent | Component<any, any>> =
Comp extends SvelteComponent<infer Props> Comp extends SvelteComponent<infer Props>
? Props ? Props
: Comp extends Component<infer Props> : Comp extends Component<infer Props, any>
? Props ? Props
: never; : never;

@ -261,6 +261,11 @@ const functionComponentProps: ComponentProps<typeof functionComponent> = {
prop: 1 prop: 1
}; };
// Test that self-typed functions are correctly inferred, too (use case: language tools has its own shape for backwards compatibility)
const functionComponentProps2: ComponentProps<(a: any, b: { a: true }) => { foo: string }> = {
a: true
};
mount(functionComponent, { mount(functionComponent, {
target: null as any as Document | Element | ShadowRoot, target: null as any as Document | Element | ShadowRoot,
props: { props: {

@ -223,10 +223,10 @@ declare module 'svelte' {
* withProps(MyComponent, { foo: 'bar' }); * withProps(MyComponent, { foo: 'bar' });
* ``` * ```
*/ */
export type ComponentProps<Comp extends SvelteComponent | Component<any>> = export type ComponentProps<Comp extends SvelteComponent | Component<any, any>> =
Comp extends SvelteComponent<infer Props> Comp extends SvelteComponent<infer Props>
? Props ? Props
: Comp extends Component<infer Props> : Comp extends Component<infer Props, any>
? Props ? Props
: never; : never;

Loading…
Cancel
Save