Skip to content
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

Closed
sevensc opened this issue Mar 8, 2017 · 4 comments
Closed

tsc returns base class instead of "this" class #14547

sevensc opened this issue Mar 8, 2017 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@sevensc
Copy link

sevensc commented Mar 8, 2017

TypeScript Version: 2.2.1
You can test the issue with this: https://github.com/sevensc/linqscript

This is my class:

    export class List<T> extends Array<T> {}

When compiling with latest typescript version on Mac "tsc source.ts", the source.js function looks like this:

function List() {
            return _super !== null && _super.apply(this, arguments) || this;
        }

which returns a new instance of an "Array", instead of an instance of a "List"

Actual behavior:

var list = new List<string>() // -> list = []

Anyhow, when i change the function to:

function List() {
            _super !== null && _super.apply(this, arguments) || this;
        }

or

function List() {
           return this;
        }

everything works just fine.

Expected behavior:

var list = new List<string>() // -> list = List {}
@RyanCavanaugh
Copy link
Member

https://github.com/Microsoft/TypeScript/wiki/FAQ#why-doesnt-extending-built-ins-like-error-array-and-map-work

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Mar 9, 2017
@sevensc
Copy link
Author

sevensc commented Mar 9, 2017

@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
Object.setPrototypeOf(this, FooError.prototype); ?

@DanielRosenwasser
Copy link
Member

One thing you could do is rewrite as an ES5-style class and set up the prototype chain between the two manually.

@sevensc
Copy link
Author

sevensc commented Mar 18, 2017

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

@sevensc sevensc closed this as completed Mar 18, 2017
sevensc pushed a commit to sevensc/linqscript that referenced this issue Mar 18, 2017
… 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.
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants