Last active
September 19, 2020 05:44
-
-
Save duarten/00b62403963b9ef3fa56dd5ca552e585 to your computer and use it in GitHub Desktop.
Tuple type containing only unique values
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 ElementOf<T> = T extends (infer E)[] ? E : never; | |
type TupleHead<Tuple extends readonly unknown[]> = | |
Tuple extends [infer HeadElement, ...readonly unknown[]] ? HeadElement : never; | |
type TupleTail<Tuple extends readonly unknown[]> = | |
Tuple extends [unknown, ...infer TailElements] ? TailElements : never; | |
type Unique<Tuple extends readonly unknown[]> | |
= Tuple extends [] | |
? [] | |
: (TupleHead<Tuple> extends ElementOf<TupleTail<Tuple>> | |
? never | |
: [TupleHead<Tuple>, ...Unique<TupleTail<Tuple>>]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment