How to use prototype in typescript

I use the fill function first

const range = new Array(layernum.length).fill(NaN);//[ts] Property 'fill' does not exist on type 'any[]'

to solve this problem, I use

const range = new Int32Array(layernum.length).fill(NaN);

instead

while it causes another problem

let layer = range.map(e => range.map(e => e)); //Type 'Int32Array' is not assignable to type 'number'

so how to use prototype in typescript

+4
source share
2 answers

Array method fillexists only in ES6 or higher. In order for typescript to recognize the version of the Arper class for propper ES6, you need to include it es6in libyour property tsconfig.json. For instance:

{
    "compilerOptions": {
        "target": "es5",
        "lib" : ["es6", "dom"]
    }
}
+4
source

If you do not want / cannot use lib.es6.d.ts, then you can update the compiler using the method signature :

declare global {
    interface Array<T> {
        fill(value: T, start?: number, end?: number): this;
    }
}
+3
source

Source: https://habr.com/ru/post/1659580/


All Articles