Type Safety with ES2022's Array.prototype.at

Ben Asher
Ben Asher
Engineering

1 minute read

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[] = [];
2
3arr[0].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 toString(). With .at(), we get a compiler error:

Typescripts Compiler Error
Typescript'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

Subscribe to Updates

Ashby products are trusted by recruiting teams at fastย growingย companies.

QuoraVanMoofDockerWeTransferIroncladDeelRampVeevFullStoryJuniAstronomerTalentfulModern Treasury
QuoraVanMoofDockerWeTransferIroncladDeelRampVeevFullStoryJuniAstronomerTalentfulModern Treasury
NotionVerkadaRetoolMarqetaDuolingoRampColorDeliveroo
NotionVerkadaRetoolMarqetaDuolingoRampColorDeliveroo