DEV Community

Discussion on: How 2 TypeScript: Get the last item type from a tuple of types

Collapse
 
sno2 profile image
Carter Snook • Edited

For the GetLength type, you can actually just access the length property via bracket notation and receive the same result:

type GetLength<T extends unknown[]> = T["length"];
Enter fullscreen mode Exit fullscreen mode

I would recommend this method more because it looks like it's intrinsic behavior built within TypeScript to set that and I would assume it would also be faster and result in less type computations. Finally, it would also get rid of the never fail-safe in the original GetLength type.