Skip to content

Commit 7699696

Browse files
committed
feat: Define the MountOptions type
1 parent 5deb37e commit 7699696

File tree

3 files changed

+76
-39
lines changed

3 files changed

+76
-39
lines changed

packages/svelte/src/index.d.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,40 @@ export interface EventDispatcher<EventMap extends Record<string, any>> {
309309
): boolean;
310310
}
311311

312+
/**
313+
* Defines the options accepted by the `mount()` function.
314+
*/
315+
export type MountOptions<Props extends Record<string, any> = Record<string, any>> = {
316+
/**
317+
* Target element where the component will be mounted.
318+
*/
319+
target: Document | Element | ShadowRoot;
320+
/**
321+
* Optional node inside `target` and when specified, it is used to render the component immediately before it.
322+
*/
323+
anchor?: Node;
324+
/**
325+
* Allows the specification of events.
326+
*/
327+
events?: Record<string, (e: any) => any>;
328+
/**
329+
* Used to define context at the component level.
330+
*/
331+
context?: Map<any, any>;
332+
/**
333+
* Used to control transition playback on initial render. The default value is `true` to run transitions.
334+
*/
335+
intro?: boolean;
336+
} & ({} extends Props ? {
337+
/**
338+
* Component properties.
339+
*/
340+
props?: Props;
341+
} : {
342+
/**
343+
* Component properties.
344+
*/
345+
props: Props;
346+
})
347+
312348
export * from './index-client.js';

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @import { ComponentContext, Effect, TemplateNode } from '#client' */
2-
/** @import { Component, ComponentType, SvelteComponent } from '../../index.js' */
2+
/** @import { Component, ComponentType, SvelteComponent, MountOptions } from '../../index.js' */
33
import { DEV } from 'esm-env';
44
import {
55
clear_text_content,
@@ -65,21 +65,7 @@ export function set_text(text, value) {
6565
* @template {Record<string, any>} Props
6666
* @template {Record<string, any>} Exports
6767
* @param {ComponentType<SvelteComponent<Props>> | Component<Props, Exports, any>} component
68-
* @param {{} extends Props ? {
69-
* target: Document | Element | ShadowRoot;
70-
* anchor?: Node;
71-
* props?: Props;
72-
* events?: Record<string, (e: any) => any>;
73-
* context?: Map<any, any>;
74-
* intro?: boolean;
75-
* }: {
76-
* target: Document | Element | ShadowRoot;
77-
* props: Props;
78-
* anchor?: Node;
79-
* events?: Record<string, (e: any) => any>;
80-
* context?: Map<any, any>;
81-
* intro?: boolean;
82-
* }} options
68+
* @param {MountOptions<Props>} options
8369
* @returns {Exports}
8470
*/
8571
export function mount(component, options) {
@@ -175,14 +161,7 @@ const document_listeners = new Map();
175161
/**
176162
* @template {Record<string, any>} Exports
177163
* @param {ComponentType<SvelteComponent<any>> | Component<any>} Component
178-
* @param {{
179-
* target: Document | Element | ShadowRoot;
180-
* anchor?: Node;
181-
* props?: any;
182-
* events?: any;
183-
* context?: Map<any, any>;
184-
* intro?: boolean;
185-
* }} options
164+
* @param {MountOptions} options
186165
* @returns {Exports}
187166
*/
188167
function _mount(Component, { target, anchor, props = {}, events, context, intro = true }) {

packages/svelte/types/index.d.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,42 @@ declare module 'svelte' {
305305
: [type: Type, parameter: EventMap[Type], options?: DispatchOptions]
306306
): boolean;
307307
}
308+
309+
/**
310+
* Defines the options accepted by the `mount()` function.
311+
*/
312+
export type MountOptions<Props extends Record<string, any> = Record<string, any>> = {
313+
/**
314+
* Target element where the component will be mounted.
315+
*/
316+
target: Document | Element | ShadowRoot;
317+
/**
318+
* Optional node inside `target` and when specified, it is used to render the component immediately before it.
319+
*/
320+
anchor?: Node;
321+
/**
322+
* Allows the specification of events.
323+
*/
324+
events?: Record<string, (e: any) => any>;
325+
/**
326+
* Used to define context at the component level.
327+
*/
328+
context?: Map<any, any>;
329+
/**
330+
* Used to control transition playback on initial render. The default value is `true` to run transitions.
331+
*/
332+
intro?: boolean;
333+
} & ({} extends Props ? {
334+
/**
335+
* Component properties.
336+
*/
337+
props?: Props;
338+
} : {
339+
/**
340+
* Component properties.
341+
*/
342+
props: Props;
343+
})
308344
/**
309345
* The `onMount` function schedules a callback to run as soon as the component has been mounted to the DOM.
310346
* It must be called during the component's initialisation (but doesn't need to live *inside* the component;
@@ -384,21 +420,7 @@ declare module 'svelte' {
384420
* Transitions will play during the initial render unless the `intro` option is set to `false`.
385421
*
386422
* */
387-
export function mount<Props extends Record<string, any>, Exports extends Record<string, any>>(component: ComponentType<SvelteComponent<Props>> | Component<Props, Exports, any>, options: {} extends Props ? {
388-
target: Document | Element | ShadowRoot;
389-
anchor?: Node;
390-
props?: Props;
391-
events?: Record<string, (e: any) => any>;
392-
context?: Map<any, any>;
393-
intro?: boolean;
394-
} : {
395-
target: Document | Element | ShadowRoot;
396-
props: Props;
397-
anchor?: Node;
398-
events?: Record<string, (e: any) => any>;
399-
context?: Map<any, any>;
400-
intro?: boolean;
401-
}): Exports;
423+
export function mount<Props extends Record<string, any>, Exports extends Record<string, any>>(component: ComponentType<SvelteComponent<Props>> | Component<Props, Exports, any>, options: MountOptions<Props>): Exports;
402424
/**
403425
* Hydrates a component on the given target and returns the exports and potentially the props (if compiled with `accessors: true`) of the component
404426
*

0 commit comments

Comments
 (0)