Type Safety with ES2022's Array.prototype.at
1 minute read
Array.prototype.at and we've come to love using it.
Array.prototype.at can replace using the standard index operator
1const arr: number = ; 2 3arr.toString(); 4 5// Same as above! 6arr.at(0).toString();
In the example above, even though index 0 may not have anything there, Typescript is happy to allow us to to call
.at(), we get a compiler error:
Array.prototype.at is not officially supported in all versions of Node. Check node.green to see if you can use it!