Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use prototype in typescript

Tags:

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

like image 833
Cery Avatar asked Mar 10 '23 18:03

Cery


2 Answers

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;
    }
}
like image 34
Nitzan Tomer Avatar answered Apr 01 '23 02:04

Nitzan Tomer


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"]
    }
}
like image 132
Daniel Tabuenca Avatar answered Apr 01 '23 02:04

Daniel Tabuenca