Skip to content

Commit 2919de1

Browse files
committed
docs: demonstrate how to infer a component's props type
1 parent 95d07de commit 2919de1

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

packages/svelte/src/index.d.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,31 @@ export type ComponentEvents<Comp extends SvelteComponent> =
188188
Comp extends SvelteComponent<any, infer Events> ? Events : never;
189189

190190
/**
191-
* Convenience type to get the props the given component expects. Example:
192-
* ```html
193-
* <script lang="ts">
194-
* import type { ComponentProps } from 'svelte';
195-
* import Component from './Component.svelte';
191+
* Convenience type to get the props the given component expects.
196192
*
197-
* const props: ComponentProps<Component> = { foo: 'bar' }; // Errors if these aren't the correct props
198-
* </script>
193+
* Example: Ensure a variable contains the props expected by `MyComponent`:
194+
*
195+
* ```ts
196+
* import type { ComponentProps } from 'svelte';
197+
* import MyComponent from './MyComponent.svelte';
198+
*
199+
* // Errors if these aren't the correct props expected by MyComponent.
200+
* const props: ComponentProps<MyComponent> = { foo: 'bar' };
201+
* ```
202+
*
203+
* Example: A generic function that accepts some component and infers the type of its props:
204+
*
205+
* ```ts
206+
* import type { Component, ComponentProps } from 'svelte';
207+
* import MyComponent from './MyComponent.svelte';
208+
*
209+
* function withProps<TComponent extends Component<any>>(
210+
* component: TComponent,
211+
* props: ComponentProps<TComponent>
212+
* ) {};
213+
*
214+
* // Errors if the second argument is not the correct props expected by the component in the first argument.
215+
* withProps(MyComponent, { foo: 'bar' });
199216
* ```
200217
*/
201218
export type ComponentProps<Comp extends SvelteComponent | Component<any>> =

packages/svelte/types/index.d.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,31 @@ declare module 'svelte' {
185185
Comp extends SvelteComponent<any, infer Events> ? Events : never;
186186

187187
/**
188-
* Convenience type to get the props the given component expects. Example:
189-
* ```html
190-
* <script lang="ts">
191-
* import type { ComponentProps } from 'svelte';
192-
* import Component from './Component.svelte';
188+
* Convenience type to get the props the given component expects.
193189
*
194-
* const props: ComponentProps<Component> = { foo: 'bar' }; // Errors if these aren't the correct props
195-
* </script>
190+
* Example: Ensure a variable contains the props expected by `MyComponent`:
191+
*
192+
* ```ts
193+
* import type { ComponentProps } from 'svelte';
194+
* import MyComponent from './MyComponent.svelte';
195+
*
196+
* // Errors if these aren't the correct props expected by MyComponent.
197+
* const props: ComponentProps<MyComponent> = { foo: 'bar' };
198+
* ```
199+
*
200+
* Example: A generic function that accepts some component and infers the type of its props:
201+
*
202+
* ```ts
203+
* import type { Component, ComponentProps } from 'svelte';
204+
* import MyComponent from './MyComponent.svelte';
205+
*
206+
* function withProps<TComponent extends Component<any>>(
207+
* component: TComponent,
208+
* props: ComponentProps<TComponent>
209+
* ) {};
210+
*
211+
* // Errors if the second argument is not the correct props expected by the component in the first argument.
212+
* withProps(MyComponent, { foo: 'bar' });
196213
* ```
197214
*/
198215
export type ComponentProps<Comp extends SvelteComponent | Component<any>> =

0 commit comments

Comments
 (0)