Skip to content

Commit

Permalink
TupleToUnion: Support arrays (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorade authored Nov 5, 2022
1 parent e11ab80 commit a68f6b2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion source/tuple-to-union.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ type NumberBool = typeof numberBool[number];
@category Array
*/
export type TupleToUnion<ArrayType> = ArrayType extends readonly [infer Head, ...(infer Rest)] ? Head | TupleToUnion<Rest> : never;
export type TupleToUnion<ArrayType> = ArrayType extends readonly unknown[] ? ArrayType[number] : never;
9 changes: 9 additions & 0 deletions test-d/tuple-to-union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ expectType<'c'>(c);

declare const notAnArray: TupleToUnion<[]>;
expectType<never>(notAnArray);

declare const worksWithArrays: TupleToUnion<Array<string | number>>;
expectType<string | number>(worksWithArrays);

declare const resolvesToNeverForNonArrays: TupleToUnion<string | number>;
expectType<never>(resolvesToNeverForNonArrays);

declare const infiniteRestArgs: TupleToUnion<[string, ...number[]]>;
expectType<string | number>(infiniteRestArgs);

0 comments on commit a68f6b2

Please sign in to comment.