Skip to content

Commit

Permalink
allow optional parameters for createEventDispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanhofer committed Feb 4, 2022
1 parent c17384b commit 9a34d89
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/runtime/internal/lifecycle.ts
Expand Up @@ -36,6 +36,8 @@ 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
: undefined extends EventMap[EventKey]
? (type: EventKey, detail?: EventMap[EventKey]) => void
: (type: EventKey, detail: EventMap[EventKey]) => void

type CreateDispatchFunctionMap<EventMap> = {
Expand Down Expand Up @@ -63,6 +65,30 @@ export function createEventDispatcher<
}) as EventDispatcher<EventMap>;
}


const dispatch = createEventDispatcher<{
loaded: never
change: string
valid: boolean
}>();

// @ts-expect-error
dispatch('some-event');

dispatch('loaded');
// @ts-expect-error
dispatch('loaded', 123);

dispatch('change', 'string');
// @ts-expect-error
dispatch('change', 123);

dispatch('valid', true);
// @ts-expect-error
dispatch('valid', 'string');
dispatch('change', undefined);


export function setContext<T>(key, context: T) {
get_current_component().$$.context.set(key, context);
}
Expand Down

0 comments on commit 9a34d89

Please sign in to comment.