Array.prototype.at() is currently a stage 3 proposal, I've tried setting "lib": ["ESNext"] in my tsconfig.json, but I still got:
Property 'at' does not exist on type 'number[]'.
So how to use Array.prototype.at() in TypeScript?
And to generalize, how to use stage 3 features in TypeScript?
P.S. typescript version is v4.3.5.
Currently, Array.prototype.at() support hasn't landed in TypeScript. To use it now, we need to install @types/proposal-relative-indexing-method and include the corresponding polyfill.
The same steps apply to the other stage 3 features if they're not already supported in TypeScript.
It is not possible to use Stage 3 features in Typescript. Not all. It generally depends on the feature. Something such as Optional Chaining required a syntax update of Typescript.
About Array.prototype.at, the lib.d.ts file for ESNext is probably not updated for such function.
Because of this, you should create a new .d.ts file in your project, and use module augmentation to extend the Array declaration and add correct interfaces for Array.prototype.at. You can look here to see several examples of how lib.es<year>.d.ts are implemented.
e.g. https://github.com/microsoft/TypeScript/blob/main/lib/lib.es2019.array.d.ts#L58
Of course, creating a new .d.ts feature will not polyfill your code to add Array.prototype.at if not available in your environment. You will still need a polyfill system, such as Babel with core-js.