Created
October 2, 2018 16:15
-
-
Save inakianduaga/8fd26c9d1a4a02dd719c3470d97e4880 to your computer and use it in GitHub Desktop.
Typesafe CustomEvents on your Frontend - StrictCustomEvent proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const strictCustomEvent = < | |
T extends keyof Omit<DocumentEventMap, defaultDocumentEventNames>, | |
D extends UnpackCustomEventPayload<DocumentEventMap[T]> | |
>( | |
name: T, | |
payload: Omit<CustomEventInit<D>, "detail"> & { | |
detail: D | |
} | |
): CustomEvent<D> => new CustomEvent(name, payload) | |
/** | |
* Extracts the detail type from a custom event type | |
*/ | |
type UnpackCustomEventPayload<T> = T extends CustomEvent<infer U> ? U : never | |
type Omit<T, K> = Pick<T, Exclude<keyof T, K>> | |
type defaultDocumentEventNames = | |
| "animationcancel" | |
| "animationend" | |
| ... // all keys from plain `DocumentEventMap` type |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment