Typings for createEventDispatcher

Makes it possible to explicitly type which events can be dispatched like so:

```ts
const bla2 = createEventDispatcher<{click: boolean}>();
bla2('click', ''); // error, type string not assignable to type boolean
bla2('qwd', true); // error, "qwd" not assignable to "click"
```

#5211
pull/5260/head
Simon H 5 years ago committed by GitHub
parent a1cb70dde4
commit 0e9158b352
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -27,7 +27,9 @@ export function onDestroy(fn) {
get_current_component().$$.on_destroy.push(fn); get_current_component().$$.on_destroy.push(fn);
} }
export function createEventDispatcher() { export function createEventDispatcher<
EventMap extends {} = any
>(): <EventKey extends Extract<keyof EventMap, string>>(type: EventKey, detail?: EventMap[EventKey]) => void {
const component = get_current_component(); const component = get_current_component();
return (type: string, detail?: any) => { return (type: string, detail?: any) => {

Loading…
Cancel
Save