Skip to content

Commit 0279546

Browse files
committed
fix: adjust broken types
The generated types were invalid TypeScript ("cannot use unique symbol at this position"). Use utility types to ensure the types are not unpacked during type generation. Leftover from #9988
1 parent d309a9d commit 0279546

File tree

5 files changed

+38
-36
lines changed

5 files changed

+38
-36
lines changed

packages/svelte/src/internal/client/render.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,7 +2759,7 @@ export function spread_props(...props) {
27592759
* @template {Record<string, any>} Props
27602760
* @template {Record<string, any> | undefined} Exports
27612761
* @template {Record<string, any>} Events
2762-
* @param {typeof import('../../main/public.js').SvelteComponent<Props, Events>} component
2762+
* @param {import('../../main/public.js').ComponentType<import('../../main/public.js').SvelteComponent<Props, Events>>} component
27632763
* @param {{
27642764
* target: Node;
27652765
* props?: Props;
@@ -2809,7 +2809,7 @@ export function createRoot(component, options) {
28092809
* @template {Record<string, any>} Props
28102810
* @template {Record<string, any> | undefined} Exports
28112811
* @template {Record<string, any>} Events
2812-
* @param {typeof import('../../main/public.js').SvelteComponent<Props, Events>} component
2812+
* @param {import('../../main/public.js').ComponentType<import('../../main/public.js').SvelteComponent<Props, Events>>} component
28132813
* @param {{
28142814
* target: Node;
28152815
* props?: Props;

packages/svelte/src/legacy/legacy-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function createClassComponent(options) {
3434
* @template {Record<string, any>} Slots
3535
*
3636
* @param {import('../main/public.js').SvelteComponent<Props, Events, Slots>} component
37-
* @returns {typeof import('../main/public.js').SvelteComponent<Props, Events, Slots> & Exports}
37+
* @returns {import('../main/public.js').ComponentType<import('../main/public.js').SvelteComponent<Props, Events, Slots> & Exports>}
3838
*/
3939
export function asClassComponent(component) {
4040
// @ts-expect-error $$prop_def etc are not actually defined

packages/svelte/src/legacy/legacy-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function asClassComponent(component) {
3030
html: result.html
3131
};
3232
};
33-
// this is present for SSR
33+
// @ts-expect-error this is present for SSR
3434
component_constructor.render = _render;
3535

3636
// @ts-ignore

packages/svelte/src/main/public.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type PropsWithChildren<Props, Slots> = Props &
2323
(Props extends { children?: any }
2424
? {}
2525
: Slots extends { default: any }
26-
? { children?: Snippet<[]> }
26+
? { children?: Snippet }
2727
: {});
2828

2929
/**

packages/svelte/types/index.d.ts

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ declare module 'svelte' {
2424
(Props extends { children?: any }
2525
? {}
2626
: Slots extends { default: any }
27-
? { children?: Snippet<[]> }
27+
? { children?: Snippet }
2828
: {});
2929

3030
/**
@@ -321,15 +321,7 @@ declare module 'svelte' {
321321
* If you don't need to interact with the component after mounting, use `mount` instead to save some bytes.
322322
*
323323
* */
324-
export function createRoot<Props extends Record<string, any>, Exports extends Record<string, any> | undefined, Events extends Record<string, any>>(component: {
325-
new (options: ComponentConstructorOptions<Props & (Props extends {
326-
children?: any;
327-
} ? {} : {} | {
328-
children?: ((this: void) => unique symbol & {
329-
_: "functions passed to {@render ...} tags must use the `Snippet` type imported from \"svelte\"";
330-
}) | undefined;
331-
})>): SvelteComponent<Props, Events, any>;
332-
}, options: {
324+
export function createRoot<Props extends Record<string, any>, Exports extends Record<string, any> | undefined, Events extends Record<string, any>>(component: ComponentType<SvelteComponent<Props, Events, any>>, options: {
333325
target: Node;
334326
props?: Props | undefined;
335327
events?: Events | undefined;
@@ -346,15 +338,7 @@ declare module 'svelte' {
346338
* If you need to interact with the component after mounting, use `createRoot` instead.
347339
*
348340
* */
349-
export function mount<Props extends Record<string, any>, Exports extends Record<string, any> | undefined, Events extends Record<string, any>>(component: {
350-
new (options: ComponentConstructorOptions<Props & (Props extends {
351-
children?: any;
352-
} ? {} : {} | {
353-
children?: ((this: void) => unique symbol & {
354-
_: "functions passed to {@render ...} tags must use the `Snippet` type imported from \"svelte\"";
355-
}) | undefined;
356-
})>): SvelteComponent<Props, Events, any>;
357-
}, options: {
341+
export function mount<Props extends Record<string, any>, Exports extends Record<string, any> | undefined, Events extends Record<string, any>>(component: ComponentType<SvelteComponent<Props, Events, any>>, options: {
358342
target: Node;
359343
props?: Props | undefined;
360344
events?: Events | undefined;
@@ -1734,17 +1718,7 @@ declare module 'svelte/legacy' {
17341718
* @deprecated Use this only as a temporary solution to migrate your imperative component code to Svelte 5.
17351719
*
17361720
* */
1737-
export function asClassComponent<Props extends Record<string, any>, Exports extends Record<string, any>, Events extends Record<string, any>, Slots extends Record<string, any>>(component: SvelteComponent<Props, Events, Slots>): {
1738-
new (options: ComponentConstructorOptions<Props & (Props extends {
1739-
children?: any;
1740-
} ? {} : Slots extends {
1741-
default: any;
1742-
} ? {
1743-
children?: ((this: void) => unique symbol & {
1744-
_: "functions passed to {@render ...} tags must use the `Snippet` type imported from \"svelte\"";
1745-
}) | undefined;
1746-
} : {})>): SvelteComponent<Props, Events, Slots>;
1747-
} & Exports;
1721+
export function asClassComponent<Props extends Record<string, any>, Exports extends Record<string, any>, Events extends Record<string, any>, Slots extends Record<string, any>>(component: SvelteComponent<Props, Events, Slots>): ComponentType<SvelteComponent<Props, Events, Slots> & Exports>;
17481722
// This should contain all the public interfaces (not all of them are actually importable, check current Svelte for which ones are).
17491723

17501724
/**
@@ -1770,7 +1744,7 @@ declare module 'svelte/legacy' {
17701744
(Props extends { children?: any }
17711745
? {}
17721746
: Slots extends { default: any }
1773-
? { children?: Snippet<[]> }
1747+
? { children?: Snippet }
17741748
: {});
17751749

17761750
/**
@@ -1859,6 +1833,34 @@ declare module 'svelte/legacy' {
18591833
$set(props: Partial<Props>): void;
18601834
}
18611835

1836+
/**
1837+
* Convenience type to get the type of a Svelte component. Useful for example in combination with
1838+
* dynamic components using `<svelte:component>`.
1839+
*
1840+
* Example:
1841+
* ```html
1842+
* <script lang="ts">
1843+
* import type { ComponentType, SvelteComponent } from 'svelte';
1844+
* import Component1 from './Component1.svelte';
1845+
* import Component2 from './Component2.svelte';
1846+
*
1847+
* const component: ComponentType = someLogic() ? Component1 : Component2;
1848+
* const componentOfCertainSubType: ComponentType<SvelteComponent<{ needsThisProp: string }>> = someLogic() ? Component1 : Component2;
1849+
* </script>
1850+
*
1851+
* <svelte:component this={component} />
1852+
* <svelte:component this={componentOfCertainSubType} needsThisProp="hello" />
1853+
* ```
1854+
*/
1855+
type ComponentType<Comp extends SvelteComponent = SvelteComponent> = (new (
1856+
options: ComponentConstructorOptions<
1857+
Comp extends SvelteComponent<infer Props> ? Props : Record<string, any>
1858+
>
1859+
) => Comp) & {
1860+
/** The custom element version of the component. Only present if compiled with the `customElement` compiler option */
1861+
element?: typeof HTMLElement;
1862+
};
1863+
18621864
const SnippetReturn: unique symbol;
18631865

18641866
/**

0 commit comments

Comments
 (0)