How can I create an array of zeros in typescript?
3 answers
You can add to a public interface Array<T>
to make the method fill
available. There is a polyfill for Array.prototype.fill on MDN if you need older browser support.
interface Array<T> {
fill(value: T): Array<T>;
}
var arr = Array<number>(10).fill(0);
Eventually the method fill
will find its way into the file lib.d.ts
and you can delete it (the compiler will warn you when you need it).
+4
source to share