[feat] add convenience type for `ComponentEvents` (#7702)

pull/7707/head
Hofer Ivan 2 years ago committed by GitHub
parent 01ba78a6fb
commit bb02a22d3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,7 +16,7 @@ function modify(path, modifyFn) {
modify(
'types/runtime/index.d.ts',
content => content.replace('SvelteComponentTyped', 'SvelteComponentTyped, ComponentType, ComponentConstructorOptions, ComponentProps')
content => content.replace('SvelteComponentTyped', 'SvelteComponentTyped, ComponentType, ComponentConstructorOptions, ComponentProps, ComponentEvents')
);
modify(
'types/compiler/index.d.ts',

@ -301,6 +301,24 @@ export type ComponentProps<Component extends SvelteComponent> = Component extend
? Props
: never;
/**
* Convenience type to get the events the given component expects. Example:
* ```html
* <script lang="ts">
* import type { ComponentEvents } from 'svelte';
* import Component from './Component.svelte';
*
* function handleCloseEvent(event: ComponentEvents<Component>['close']) {
* console.log(event.detail);
* }
* </script>
*
* <Component on:close={handleCloseEvent} />
* ```
*/
export type ComponentEvents<Component extends SvelteComponent> =
Component extends SvelteComponentTyped<any, infer Events> ? Events : never;
export function loop_guard(timeout) {
const start = Date.now();
return () => {

Loading…
Cancel
Save