I've just came across a TypeScript exercise, requiring the user to define custom LastElementOfArray
type. The solution looks like this:
type Last<A extends Array<any>> = A extends [...infer _, infer L] ? L : never
But something is weird. If the used syntax is 'spread', why is there an inconsistency with how JavaScript handles 'spread'? Specifically, the spread variable must be the last one - this JavaScript code returns a SyntaxError
:
const a = [1,2,3,4,5];
(([...rest, e4, e5])=>{console.error(e4,e5)})(a)
Why does this 'inconsistency' exist? Is there any documentation on this? I've looked but found nothing.
Any help is appreacited.