Skip to content

Commit f51bd29

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

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
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 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>> =

0 commit comments

Comments
 (0)