improve types for `createEventDispatcher`

pull/7324/head
Ivan Hofer 5 years ago
parent eb6fb66f19
commit c17384b834

@ -27,12 +27,29 @@ export function onDestroy(fn: () => any) {
get_current_component().$$.on_destroy.push(fn); get_current_component().$$.on_destroy.push(fn);
} }
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void
? I
: never
type ExtractObjectValues<Object extends Record<any, any>> = Object[keyof Object]
type ConstructDispatchFunction<EventMap extends Record<string, any>, EventKey extends keyof EventMap> =
EventMap[EventKey] extends never
? (type: EventKey) => void
: (type: EventKey, detail: EventMap[EventKey]) => void
type CreateDispatchFunctionMap<EventMap> = {
[Key in keyof EventMap]: ConstructDispatchFunction<EventMap, Key>
}
type EventDispatcher<EventMap extends Record<string, any>> = UnionToIntersection<ExtractObjectValues<CreateDispatchFunctionMap<EventMap>>>
export function createEventDispatcher< export function createEventDispatcher<
EventMap extends {} = any EventMap extends Record<string, any> = any
>(): <EventKey extends Extract<keyof EventMap, string>>(type: EventKey, detail?: EventMap[EventKey]) => void { >(): EventDispatcher<EventMap> {
const component = get_current_component(); const component = get_current_component();
return (type: string, detail?: any) => { return ((type: string, detail?: any) => {
const callbacks = component.$$.callbacks[type]; const callbacks = component.$$.callbacks[type];
if (callbacks) { if (callbacks) {
@ -43,7 +60,7 @@ export function createEventDispatcher<
fn.call(component, event); fn.call(component, event);
}); });
} }
}; }) as EventDispatcher<EventMap>;
} }
export function setContext<T>(key, context: T) { export function setContext<T>(key, context: T) {

Loading…
Cancel
Save