Comment by nikeee
6 years ago
> It's obviously impossible for TS to guarantee that an array will not be empty at runtime,
Maybe you could use "Rest elements in tuple types" and do an overloaded signature like this:
function head<T>(...args: [T, ...any[]]): T;
function head<T>(...args: []): undefined
function head<T>(...args: [T, ...any[]] | []): T | undefined
{
return args[0];
}
(not tested)
You'd have to spread the arguments or use call/bind/apply, though.
No comments yet
Contribute on Hacker News ↗