Type Safety with ES2022's Array.prototype.at

ES2022 introduces Array.prototype.at and we've come to love using it. Array.prototype.at can replace using the standard index operator []:

1const arr: number[] = [];
5// Same as above!

In the example above, even though index 0 may not have anything there, Typescript is happy to allow us to to call toString(). With .at(), we get a compiler error:

Typescripts Compiler ErrorTypescript's compiler error.

Array.prototype.at is not officially supported in all versions of Node. Check node.green to see if you can use it!

Share this post