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);
}
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();
return (type: string, detail?: any) => {
@ -61,4 +63,4 @@ export function bubble(component, event) {
if (callbacks) {
callbacks.slice().forEach(fn => fn(event));
}
}
}

Loading…
Cancel
Save