-
-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
f120d8a
commit 1f4f7a1
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type {IsNever} from './is-never'; | ||
import type {UnionToIntersection} from './union-to-intersection'; | ||
|
||
/** | ||
Returns the last element of a union type. | ||
@example | ||
``` | ||
type Last = LastOfUnion<1 | 2 | 3>; | ||
//=> 3 | ||
``` | ||
*/ | ||
type LastOfUnion<T> = | ||
UnionToIntersection<T extends any ? () => T : never> extends () => (infer R) | ||
? R | ||
: never; | ||
|
||
/** | ||
Convert a union type into an unordered tuple type of its elements. | ||
This can be useful when you have objects with a finite set of keys and want a type defining only the allowed keys, but do not want to repeat yourself. | ||
@example | ||
``` | ||
import type {UnionToTuple} from 'type-fest'; | ||
type Numbers = 1 | 2 | 3; | ||
type NumbersTuple = UnionToTuple<Numbers>; | ||
//=> [1, 2, 3] | ||
``` | ||
@example | ||
``` | ||
import type {UnionToTuple} from 'type-fest'; | ||
const pets = { | ||
dog: '🐶', | ||
cat: '🐱', | ||
snake: '🐍', | ||
}; | ||
type Pet = keyof typeof pets; | ||
//=> 'dog' | 'cat' | 'snake' | ||
const petList = Object.keys(pets) as UnionToTuple<Pet>; | ||
//=> ['dog', 'cat', 'snake'] | ||
``` | ||
@category Array | ||
*/ | ||
export type UnionToTuple<T, L = LastOfUnion<T>> = | ||
IsNever<T> extends false | ||
? [...UnionToTuple<Exclude<T, L>>, L] | ||
: []; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {expectAssignable, expectError, expectType} from 'tsd'; | ||
import type {UnionToTuple} from '../index'; | ||
|
||
type Options = UnionToTuple<'a' | 'b' | 'c'>; | ||
// Results unordered | ||
expectAssignable<['a', 'b', 'c'] | ['a', 'c', 'b'] | ['b', 'a', 'c'] | ['b', 'c', 'a'] | ['c', 'a', 'b'] | ['c', 'b', 'a']>({} as Options); | ||
expectType<Options[number]>({} as ('a' | 'b' | 'c')); | ||
|
||
type Options1 = UnionToTuple<1 | 2 | 3>; | ||
expectType<Options1[number]>({} as (1 | 2 | 3)); | ||
|
||
type Options2 = UnionToTuple<boolean | 1>; | ||
expectType<Options2[number]>({} as (1 | false | true)); |