fix: type-level back-compat for default slot and children prop

If someone has an existing SvelteComponent type definition and using the slot generic to type the default slot, automatically add that slot to the prop type as "children"
pull/9633/head
Simon Holthausen 1 year ago
parent cb4b1f0a18
commit a3bc7d5698

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: type-level back-compat for default slot and children prop

@ -18,6 +18,14 @@ export interface ComponentConstructorOptions<
$$inline?: boolean;
}
// Utility type for ensuring backwards compatibility on a type level: If there's a default slot, add 'children' to the props if it doesn't exist there already
type PropsWithChildren<Props, Slots> = Props &
(Props extends { children?: any }
? {}
: Slots extends { default: any }
? { children?: Snippet }
: {});
/**
* Can be used to create strongly typed Svelte components.
*
@ -57,13 +65,13 @@ export class SvelteComponent<
* is a stop-gap solution. Migrate towards using `mount` or `createRoot` instead. See
* https://svelte-5-preview.vercel.app/docs/breaking-changes#components-are-no-longer-classes for more info.
*/
constructor(options: ComponentConstructorOptions<Props>);
constructor(options: ComponentConstructorOptions<PropsWithChildren<Props, Slots>>);
/**
* For type checking capabilities only.
* Does not exist at runtime.
* ### DO NOT USE!
* */
$$prop_def: Props;
$$prop_def: PropsWithChildren<Props, Slots>;
/**
* For type checking capabilities only.
* Does not exist at runtime.

@ -15,11 +15,11 @@ class LegacyComponent extends SvelteComponent<
{ slot: { slotProps: boolean } }
> {}
// @ts-expect-error
const legacyComponent = new LegacyComponent({
target: null as any as Document | Element | ShadowRoot,
props: {
prop: 'foo',
// @ts-expect-error
x: ''
}
});
@ -56,14 +56,20 @@ class NewComponent extends SvelteComponent<
anExport: string = '';
}
// @ts-expect-error
new NewComponent({
prop: 'foo',
x: ''
target: null as any,
props: {
prop: 'foo',
// @ts-expect-error
x: ''
}
});
const newComponent: NewComponent = new NewComponent({
prop: 'foo'
target: null as any,
props: {
prop: 'foo'
}
});
newComponent.$$events_def.event;
// @ts-expect-error
@ -123,11 +129,11 @@ instance.anExport === 1;
// --------------------------------------------------------------------------- interop
const AsLegacyComponent = asClassComponent(newComponent);
// @ts-expect-error
new AsLegacyComponent({
target: null as any,
props: {
prop: '',
// @ts-expect-error
x: ''
}
});

Loading…
Cancel
Save