Skip to content

Commit

Permalink
updated init and tail typings
Browse files Browse the repository at this point in the history
  • Loading branch information
Harris-Miller committed Apr 2, 2024
1 parent 312a066 commit 598fdd6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 9 additions & 1 deletion types/init.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
// string
export function init(list: string): string;
export function init<T>(list: readonly T[]): T[];
// empty tuple - purposefully `never, They literally have no init
export function tail(list: readonly []): never;
// length=1 tuples also return `never`. They literally have no init
export function init<T>(list: readonly [T]): never;
// non-empty tuples and array
// `infer Init` only works on types like `readonly [1, '2', 3]` where you will get back `['2', 3]`
// else, if the type is `string[]`, you'll get back `string[]`
export function init<T extends readonly [...any]>(list: T): T extends readonly [...infer Init, any] ? Init : T;
4 changes: 2 additions & 2 deletions types/tail.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export function tail(list: readonly []): never;
// length=1 tuples also return `never`. They literally have no tail
export function tail<T>(list: readonly [T]): never;
// non-empty tuples and array
// `infer Rest` only works on types like `readonly [1, '2', 3]` where you will get back `['2', 3]`
// `infer Tail` only works on types like `readonly [1, '2', 3]` where you will get back `['2', 3]`
// else, if the type is `string[]`, you'll get back `string[]`
export function tail<T extends readonly [...any]>(tuple: T): T extends readonly [any, ...infer Rest] ? Rest : T;
export function tail<T extends readonly [...any]>(list: T): T extends readonly [any, ...infer Tail] ? Tail : T;

0 comments on commit 598fdd6

Please sign in to comment.