Skip to content

Instantly share code, notes, and snippets.

@reslear
Created December 24, 2021 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reslear/f7b85e50234be225e7c8f256fd3ff049 to your computer and use it in GitHub Desktop.
Save reslear/f7b85e50234be225e7c8f256fd3ff049 to your computer and use it in GitHub Desktop.
Space To Underscore type infer
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