Hey! Great article. In case it's useful, I figured out a way to get the length - 1 of an arbitrary-length tuple, so you're no longer limited to the hard-coded breadth of Prev<T>! Check it out:
// Gets the length of an array/tuple type. Example://// type FooLength = LengthOfTuple<[string, number, boolean]>;// //=> 3//exporttypeLengthOfTuple<Textendsany[]>=Textends{length:inferL}?L:never;// Drops the first element of a tuple. Example://// type Foo = DropFirstInTuple<[string, number, boolean]>;// //=> [number, boolean]//exporttypeDropFirstInTuple<Textendsany[]>=((...args:T)=>any)extends(arg:any,...rest:inferU)=>any?U:T;// Gets the type of the last element of a tuple. Example://// type Foo = LastInTuple<[string, number, boolean]>;// //=> boolean//// function lastArg<T extends any[]>(...args: T): LastInTuple<T> {// return args[args.length - 1];// }//// const bar = lastArg(1);// type Bar = typeof bar;// //=> number//// const baz = lastArg(1, true, "hey", 123, 1, 2, 3, 4, 5, 6, 7, -1, false);// type Baz = typeof baz;// //=> boolean//exporttypeLastInTuple<Textendsany[]>=T[LengthOfTuple<DropFirstInTuple<T>>];
Hey! Great article. In case it's useful, I figured out a way to get the
length - 1
of an arbitrary-length tuple, so you're no longer limited to the hard-coded breadth ofPrev<T>
! Check it out:This magic spell has just saved me a bunch of headache. Thanks, Keegan!
Interesting I like the way you removed the first item from the tuple.
It can be simplified more by doing this: