Created
December 24, 2021 07:51
-
-
Save reslear/f7b85e50234be225e7c8f256fd3ff049 to your computer and use it in GitHub Desktop.
Space To Underscore type infer
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
const Names = [ | |
'Event Update' | |
] | |
type ToUnderscore<V extends string> = V extends `${infer S} ${infer E}` | |
? `${S}_${ToUnderscore<E>}` | |
: V | |
type TNames<N extends string> = { [K in N as Uppercase<ToUnderscore<N>>]: K } | |
const EventNames = Names.reduce( | |
(a, v) => ({ ...a, [v.toUpperCase().replace(/\s+/, '_')]: v }), | |
Object.create({}) | |
) as TNames<typeof Names[number]> | |
const a = EventNames.APP_OPENED_FROM_PUSH_NOTIFICATION | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment