From 598fdd6adac5d2b824e67660c6e23b7e3dcb92be Mon Sep 17 00:00:00 2001 From: harris-miller Date: Mon, 20 Nov 2023 02:40:04 -0700 Subject: [PATCH] updated init and tail typings --- types/init.d.ts | 10 +++++++++- types/tail.d.ts | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/types/init.d.ts b/types/init.d.ts index 61209676..cdebab8f 100644 --- a/types/init.d.ts +++ b/types/init.d.ts @@ -1,2 +1,10 @@ +// string export function init(list: string): string; -export function init(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(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(list: T): T extends readonly [...infer Init, any] ? Init : T; diff --git a/types/tail.d.ts b/types/tail.d.ts index f15ae378..6e1a49dc 100644 --- a/types/tail.d.ts +++ b/types/tail.d.ts @@ -5,6 +5,6 @@ export function tail(list: readonly []): never; // length=1 tuples also return `never`. They literally have no tail export function tail(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(tuple: T): T extends readonly [any, ...infer Rest] ? Rest : T; +export function tail(list: T): T extends readonly [any, ...infer Tail] ? Tail : T;