Skip to content

Commit

Permalink
ArrayTail: Fix support for optional parameters (#977)
Browse files Browse the repository at this point in the history
Co-authored-by: som-sm <som-sm@users.noreply.github.com>
  • Loading branch information
ferferga and som-sm authored Dec 2, 2024
1 parent a716c29 commit f6b1387
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion source/array-tail.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ add3(4);
@category Array
*/
export type ArrayTail<TArray extends UnknownArrayOrTuple> = TArray extends readonly [unknown, ...infer Tail] ? Tail : [];
export type ArrayTail<TArray extends UnknownArrayOrTuple> = TArray extends readonly [unknown?, ...infer Tail]
? keyof TArray & `${number}` extends never
? []
: Tail
: [];
14 changes: 14 additions & 0 deletions test-d/array-tail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,17 @@ expectType<[]>(getArrayTail(['a', 'b', 'c']));
expectType<[]>(getArrayTail([] as const));
expectType<[]>(getArrayTail(['a'] as const));
expectType<['b', 'c']>(getArrayTail(['a', 'b', 'c'] as const));

// Optional elements tests
expectType<[undefined, 'c']>(getArrayTail(['a', undefined, 'c'] as const));

// Mixed optional/required
type MixedArray = [string, undefined?, number?];
expectType<[undefined?, number?]>(getArrayTail(['hello'] as MixedArray));

// Optional numbers
expectType<[undefined, 3]>(getArrayTail([1, undefined, 3] as const));

// Complex mixed case
type ComplexArray = [string, boolean, number?, string?];
expectType<[boolean, number?, string?]>(getArrayTail(['test', false] as ComplexArray));

0 comments on commit f6b1387

Please sign in to comment.