4

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

2 Answers 2

4

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"]
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.