Skip to content

Commit 60d90a9

Browse files
authored
docs: demonstrate how to infer a component's props type (#12038)
1 parent 7a9ebb7 commit 60d90a9

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
@@ -189,14 +189,31 @@ export type ComponentEvents<Comp extends SvelteComponent> =
189189
Comp extends SvelteComponent<any, infer Events> ? Events : never;
190190

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

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

0 commit comments

Comments
 (0)