Skip to content

Commit 95980d1

Browse files
webJosetrueadm
andauthored
feat: Define the MountOptions type (#13674)
* feat: Define the MountOptions type * Revert "feat: Define the MountOptions type" This reverts commit bd3596f. * feat: Define the MountOptions type * chore: Add changeset * lint * lint --------- Co-authored-by: Dominic Gannaway <[email protected]>
1 parent d16a9da commit 95980d1

File tree

4 files changed

+85
-39
lines changed

4 files changed

+85
-39
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': minor
3+
---
4+
5+
feat: export mount() options as the MountOptions type

packages/svelte/src/index.d.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,42 @@ 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+
/**
339+
* Component properties.
340+
*/
341+
props?: Props;
342+
}
343+
: {
344+
/**
345+
* Component properties.
346+
*/
347+
props: Props;
348+
});
349+
312350
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: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,44 @@ 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+
/**
336+
* Component properties.
337+
*/
338+
props?: Props;
339+
}
340+
: {
341+
/**
342+
* Component properties.
343+
*/
344+
props: Props;
345+
});
308346
/**
309347
* The `onMount` function schedules a callback to run as soon as the component has been mounted to the DOM.
310348
* It must be called during the component's initialisation (but doesn't need to live *inside* the component;
@@ -384,21 +422,7 @@ declare module 'svelte' {
384422
* Transitions will play during the initial render unless the `intro` option is set to `false`.
385423
*
386424
* */
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;
425+
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;
402426
/**
403427
* Hydrates a component on the given target and returns the exports and potentially the props (if compiled with `accessors: true`) of the component
404428
*

0 commit comments

Comments
 (0)