Skip to content

Commit 79bdf99

Browse files
committed
same for mount/hydrate
1 parent 9d4111a commit 79bdf99

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,20 @@ export function slot(anchor, slot_fn, slot_props, fallback_fn) {
7272
* @template {Record<string, any>} Props
7373
* @template {Record<string, any>} Exports
7474
* @param {import('../../index.js').ComponentType<import('../../index.js').SvelteComponent<Props>> | import('../../index.js').Component<Props, Exports, any>} component
75-
* @param {{
75+
* @param {{} extends Props ? {
7676
* target: Document | Element | ShadowRoot;
7777
* anchor?: Node;
7878
* props?: Props;
7979
* events?: Record<string, (e: any) => any>;
8080
* context?: Map<any, any>;
8181
* intro?: boolean;
82+
* }: {
83+
* target: Document | Element | ShadowRoot;
84+
* props: Props;
85+
* anchor?: Node;
86+
* events?: Record<string, (e: any) => any>;
87+
* context?: Map<any, any>;
88+
* intro?: boolean;
8289
* }} options
8390
* @returns {Exports}
8491
*/
@@ -98,13 +105,20 @@ export function mount(component, options) {
98105
* @template {Record<string, any>} Props
99106
* @template {Record<string, any>} Exports
100107
* @param {import('../../index.js').ComponentType<import('../../index.js').SvelteComponent<Props>> | import('../../index.js').Component<Props, Exports, any>} component
101-
* @param {{
108+
* @param {{} extends Props ? {
102109
* target: Document | Element | ShadowRoot;
103110
* props?: Props;
104111
* events?: Record<string, (e: any) => any>;
105112
* context?: Map<any, any>;
106113
* intro?: boolean;
107114
* recover?: boolean;
115+
* } : {
116+
* target: Document | Element | ShadowRoot;
117+
* props: Props;
118+
* events?: Record<string, (e: any) => any>;
119+
* context?: Map<any, any>;
120+
* intro?: boolean;
121+
* recover?: boolean;
108122
* }} options
109123
* @returns {Exports}
110124
*/

packages/svelte/tests/types/component.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ mount(NewComponent, {
131131
intro: false,
132132
recover: false
133133
});
134+
mount(
135+
NewComponent,
136+
// @ts-expect-error props missing
137+
{ target: null as any }
138+
);
139+
// if component receives no args, props can be omitted
140+
mount(null as any as typeof SvelteComponent<{}>, { target: null as any });
134141

135142
hydrate(NewComponent, {
136143
target: null as any as Document | Element | ShadowRoot,
@@ -148,6 +155,13 @@ hydrate(NewComponent, {
148155
intro: false,
149156
recover: false
150157
});
158+
hydrate(
159+
NewComponent,
160+
// @ts-expect-error props missing
161+
{ target: null as any }
162+
);
163+
// if component receives no args, props can be omitted
164+
hydrate(null as any as typeof SvelteComponent<{}>, { target: null as any });
151165

152166
render(NewComponent, {
153167
props: {
@@ -263,6 +277,13 @@ mount(functionComponent, {
263277
readonly: 1
264278
}
265279
});
280+
mount(
281+
functionComponent,
282+
// @ts-expect-error props missing
283+
{ target: null as any }
284+
);
285+
// if component receives no args, props can be omitted
286+
mount(null as any as Component<{}>, { target: null as any });
266287

267288
hydrate(functionComponent, {
268289
target: null as any as Document | Element | ShadowRoot,
@@ -280,6 +301,13 @@ hydrate(functionComponent, {
280301
binding: true
281302
}
282303
});
304+
hydrate(
305+
functionComponent,
306+
// @ts-expect-error props missing
307+
{ target: null as any }
308+
);
309+
// if component receives no args, props can be omitted
310+
hydrate(null as any as Component<{}>, { target: null as any });
283311

284312
render(functionComponent, {
285313
props: {

packages/svelte/types/index.d.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,25 +350,39 @@ declare module 'svelte' {
350350
* Mounts a component to the given target and returns the exports and potentially the props (if compiled with `accessors: true`) of the component
351351
*
352352
* */
353-
export function mount<Props extends Record<string, any>, Exports extends Record<string, any>>(component: ComponentType<SvelteComponent<Props, any, any>> | Component<Props, Exports, any>, options: {
353+
export function mount<Props extends Record<string, any>, Exports extends Record<string, any>>(component: ComponentType<SvelteComponent<Props, any, any>> | Component<Props, Exports, any>, options: {} extends Props ? {
354354
target: Document | Element | ShadowRoot;
355355
anchor?: Node | undefined;
356356
props?: Props | undefined;
357357
events?: Record<string, (e: any) => any> | undefined;
358358
context?: Map<any, any> | undefined;
359359
intro?: boolean | undefined;
360+
} : {
361+
target: Document | Element | ShadowRoot;
362+
props: Props;
363+
anchor?: Node | undefined;
364+
events?: Record<string, (e: any) => any> | undefined;
365+
context?: Map<any, any> | undefined;
366+
intro?: boolean | undefined;
360367
}): Exports;
361368
/**
362369
* Hydrates a component on the given target and returns the exports and potentially the props (if compiled with `accessors: true`) of the component
363370
*
364371
* */
365-
export function hydrate<Props extends Record<string, any>, Exports extends Record<string, any>>(component: ComponentType<SvelteComponent<Props, any, any>> | Component<Props, Exports, any>, options: {
372+
export function hydrate<Props extends Record<string, any>, Exports extends Record<string, any>>(component: ComponentType<SvelteComponent<Props, any, any>> | Component<Props, Exports, any>, options: {} extends Props ? {
366373
target: Document | Element | ShadowRoot;
367374
props?: Props | undefined;
368375
events?: Record<string, (e: any) => any> | undefined;
369376
context?: Map<any, any> | undefined;
370377
intro?: boolean | undefined;
371378
recover?: boolean | undefined;
379+
} : {
380+
target: Document | Element | ShadowRoot;
381+
props: Props;
382+
events?: Record<string, (e: any) => any> | undefined;
383+
context?: Map<any, any> | undefined;
384+
intro?: boolean | undefined;
385+
recover?: boolean | undefined;
372386
}): Exports;
373387
/**
374388
* Unmounts a component that was previously mounted using `mount` or `hydrate`.

0 commit comments

Comments
 (0)