Skip to content

Instantly share code, notes, and snippets.

@tusharsnx
Created June 11, 2022 07:58
Show Gist options
  • Save tusharsnx/4e15344eca32c18762b41e15d1e10c63 to your computer and use it in GitHub Desktop.
Save tusharsnx/4e15344eca32c18762b41e15d1e10c63 to your computer and use it in GitHub Desktop.
generate tuple of primitive-types from tuple of literal-types
type TypeOfLiteral<T> = T extends string
? string
: T extends number
? number
: T extends boolean
? boolean
: T extends bigint
? bigint
: T extends Symbol
? Symbol
: T extends null
? null
: T extends undefined
? undefined
: T // if not one-of primitive, return T
type TupleOf<LiteralTuple> = LiteralTuple extends
| Readonly<[infer U, ...infer R]>
| [infer U, ...infer R]
? [TypeOfLiteral<U>, ...TupleOf<R>]
: LiteralTuple extends []
? []
: never
let temp = ["hello", true, 43] as const
let result: TupleOf<typeof temp> // [string, boolean, number]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment