-
Notifications
You must be signed in to change notification settings - Fork 12.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tsc returns base class instead of "this" class #14547
Comments
@RyanCavanaugh Thanks for the quick answer. Since i created this repo with an older version of typescript and this is in productive usage, is there any way to fix this, except for the not IE working |
One thing you could do is rewrite as an ES5-style class and set up the prototype chain between the two manually. |
Hi @DanielRosenwasser! Thanks you for your response! This i my "workaround": export class List<T> extends Array<T> {
public _array: Array<T>;
public Where(delegate: (value: T) => boolean, args?: any): List<T> {
try {
const list = new Array<T>();
if (!this.validDelegate(delegate))
return list.ToList();
for (let i = 0; i < this._array.length; i++) { //replaced 'this' with property 'this._array'
const c = this._array[i]; //replaced 'this' with property 'this._array'
if (c === null)
continue;
if (delegate.call(args, c))
list.push(c);
}
return list.ToList();
}
catch (ex) {
this._array = null;
return new List<T>();
}
}
... Interface interface Array<T> {
Where(delegate, args?);
} prototype Array.prototype.Where = function(delegate, args){
ls.List.prototype._array = this; //set public variable on class List
var returnValue = ls.List.prototype.Where(delegate, args);
ls.List.prototype._array = null; //and reset
return returnValue;
} |
… see here why: https://github.com/Microsoft/TypeScript/wiki/FAQ#why-doesnt-extending-built-ins-like-error-array-and-map-work; Refactoring, improved performance by using only native methods in class. Added summary and try catch blocks.
TypeScript Version: 2.2.1
You can test the issue with this: https://github.com/sevensc/linqscript
This is my class:
When compiling with latest typescript version on Mac "tsc source.ts", the source.js function looks like this:
which returns a new instance of an "Array", instead of an instance of a "List"
Actual behavior:
Anyhow, when i change the function to:
or
everything works just fine.
Expected behavior:
The text was updated successfully, but these errors were encountered: