Skip to content

Commit 9a34d89

Browse files
committed
allow optional parameters for createEventDispatcher
1 parent c17384b commit 9a34d89

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/runtime/internal/lifecycle.ts

+26
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type ExtractObjectValues<Object extends Record<any, any>> = Object[keyof Object]
3636
type ConstructDispatchFunction<EventMap extends Record<string, any>, EventKey extends keyof EventMap> =
3737
EventMap[EventKey] extends never
3838
? (type: EventKey) => void
39+
: undefined extends EventMap[EventKey]
40+
? (type: EventKey, detail?: EventMap[EventKey]) => void
3941
: (type: EventKey, detail: EventMap[EventKey]) => void
4042

4143
type CreateDispatchFunctionMap<EventMap> = {
@@ -63,6 +65,30 @@ export function createEventDispatcher<
6365
}) as EventDispatcher<EventMap>;
6466
}
6567

68+
69+
const dispatch = createEventDispatcher<{
70+
loaded: never
71+
change: string
72+
valid: boolean
73+
}>();
74+
75+
// @ts-expect-error
76+
dispatch('some-event');
77+
78+
dispatch('loaded');
79+
// @ts-expect-error
80+
dispatch('loaded', 123);
81+
82+
dispatch('change', 'string');
83+
// @ts-expect-error
84+
dispatch('change', 123);
85+
86+
dispatch('valid', true);
87+
// @ts-expect-error
88+
dispatch('valid', 'string');
89+
dispatch('change', undefined);
90+
91+
6692
export function setContext<T>(key, context: T) {
6793
get_current_component().$$.context.set(key, context);
6894
}

0 commit comments

Comments
 (0)