Skip to content

Commit

Permalink
feat(types): TupleToUnion
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed May 13, 2023
1 parent bb2bc85 commit 4c251c1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/types/__tests__/tuple-to-union.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file Type Tests - TupleToUnion
* @module tutils/types/tests/unit-d/TupleToUnion
*/

import type TestSubject from '../tuple-to-union'

describe('unit-d:types/TupleToUnion', () => {
it('should equal tuple elements union if T is a tuple', () => {
expectTypeOf<TestSubject<[0, 1, 2]>>().toEqualTypeOf<0 | 1 | 2>()
})

it('should equal never if T is not a tuple', () => {
expectTypeOf<TestSubject<number>>().toBeNever()
})
})
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,5 @@ export type { default as Promisable } from './promisable'
export type { default as Simplify } from './simplify'
export type { default as Split } from './split'
export type { default as Stringafiable } from './stringafiable'
export type { default as TupleToUnion } from './tuple-to-union'
export type { default as TypedArray } from './typed-array'
16 changes: 16 additions & 0 deletions src/types/tuple-to-union.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file Type Definitions - TupleToUnion
* @module tutils/types/TupleToUnion
*/

/**
* Converts a [tuple][1] to a [union][2].
*
* [1]: https://www.codecademy.com/resources/docs/typescript/tuples
* [2]: https://www.typescriptlang.org/docs/handbook/unions-and-intersections#union-types
*
* @template T - Type to evaluate
*/
type TupleToUnion<T> = T extends readonly unknown[] ? T[number] : never

export type { TupleToUnion as default }

0 comments on commit 4c251c1

Please sign in to comment.