Created
June 11, 2022 07:58
-
-
Save tusharsnx/4e15344eca32c18762b41e15d1e10c63 to your computer and use it in GitHub Desktop.
generate tuple of primitive-types from tuple of literal-types
This file contains hidden or 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
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