Skip to content

Commit

Permalink
feat(api): added the Init type
Browse files Browse the repository at this point in the history
  • Loading branch information
prescientmoon committed Jan 5, 2020
1 parent 44ae2f7 commit f6c333a
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/api/src/api/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@ export type Last<T extends unknown[]> = {
0: Head<T>;
1: Last<Tail<T>>;
}[Tail<T> extends [] ? 0 : 1];

/**
* Internal version of Init accepting 1 extra argument for the accumulated value.
*/
type InitReducer<T extends unknown[], C extends unknown[] = []> = {
0: Reverse<C>;
1: InitReducer<Tail<T>, Prepend<Head<T>, C>>;
}[Tail<T> extends [] ? 0 : 1];

/**
* Extracts everything except the last element from a tuple.
*/
export type Init<T extends unknown[]> = InitReducer<T>;

0 comments on commit f6c333a

Please sign in to comment.