Cery
Cery

Reputation: 221

how to use prototype in typescript

first i use fill function

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

to deal with this problem,i use

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

instead

while,it cause 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

Upvotes: 4

Views: 223

Answers (2)

Nitzan Tomer
Nitzan Tomer

Reputation: 164139

If you don't want/can't use the lib.es6.d.ts, then you can update the compiler with the method signature:

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

Upvotes: 3

Daniel Tabuenca
Daniel Tabuenca

Reputation: 13651

The fill method of array exists only in ES6 or above. In order for typescript to recognize the propper ES6 version of the Array class you need to make sure you include es6 in the lib property of your tsconfig.json. For example:

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

Upvotes: 4

Related Questions