it should be possible to extend build-in types in ts 1.6 as I read here:
TypeScript 1.6 adds support for classes extending arbitrary expression that computes a constructor function. This means that built-in types can now be extended in class declarations.
...
Some examples:
// Extend built-in types class MyArray extends Array<number> { } class MyError extends Error { } ...
However when extending Array, length property is not updated while using [] operator to set values. Push function works fine, but I need [] operator.
My Example:
class ExtendedArray<T> extends Array<T> {}
var buildinArray = new Array<string>();
var extendedArray = new ExtendedArray<string>();
buildinArray.push("A");
console.log(buildinArray.length); // 1 - OK
buildinArray[2] = "B";
console.log(buildinArray.length); // 3 - OK
extendedArray.push("A");
console.log(extendedArray.length); // 1 - OK
extendedArray[2] = "B";
console.log(extendedArray.length); // 1 - FAIL
console.dir(extendedArray); // both values, but wrong length
Am I doing something wrong? Where is the problem?
Array
? I've never felt the need to subclass arrays especially in TypeScript where the interfaces are extremely versatile when it comes to generics. – Ind