From 4c251c1613a7f40e5048f8f5d64c972e1d44d03d Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Sat, 13 May 2023 15:53:08 -0400 Subject: [PATCH] feat(types): `TupleToUnion` Signed-off-by: Lexus Drumgold --- src/types/__tests__/tuple-to-union.spec-d.ts | 16 ++++++++++++++++ src/types/index.ts | 1 + src/types/tuple-to-union.ts | 16 ++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 src/types/__tests__/tuple-to-union.spec-d.ts create mode 100644 src/types/tuple-to-union.ts diff --git a/src/types/__tests__/tuple-to-union.spec-d.ts b/src/types/__tests__/tuple-to-union.spec-d.ts new file mode 100644 index 00000000..1229b7dd --- /dev/null +++ b/src/types/__tests__/tuple-to-union.spec-d.ts @@ -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>().toEqualTypeOf<0 | 1 | 2>() + }) + + it('should equal never if T is not a tuple', () => { + expectTypeOf>().toBeNever() + }) +}) diff --git a/src/types/index.ts b/src/types/index.ts index 85566508..81a5e558 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -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' diff --git a/src/types/tuple-to-union.ts b/src/types/tuple-to-union.ts new file mode 100644 index 00000000..47dbad1b --- /dev/null +++ b/src/types/tuple-to-union.ts @@ -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 extends readonly unknown[] ? T[number] : never + +export type { TupleToUnion as default }