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

incorrect this and class instance with inheritation #12097

Closed
e-cloud opened this issue Nov 8, 2016 · 6 comments
Closed

incorrect this and class instance with inheritation #12097

e-cloud opened this issue Nov 8, 2016 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@e-cloud
Copy link
Contributor

e-cloud commented Nov 8, 2016

TypeScript Version: 2.1.0-dev.20161107

Code

{
  ...
  "compilerOptions": {
    "target": "es5"
  }
  ...
}
class WebpackOptionsValidationError extends Error {
    constructor() {
        super();
        Error.captureStackTrace(this, WebpackOptionsValidationError);
        this.name = 'WebpackOptionsValidationError';
    }
}

Expected behavior:

const err = new WebpackOptionsValidationError()
err instanceof WebpackOptionsValidationError // true

transpiled:

__extends(WebpackOptionsValidationError, _super);
function WebpackOptionsValidationError() {
    _super.call(this);
    Error.captureStackTrace(this, WebpackOptionsValidationError);
    this.name = 'WebpackOptionsValidationError';
    return this;
    // here return instance of WebpackOptionsValidationError
}

Actual behavior:

const err = new WebpackOptionsValidationError()
err instanceof WebpackOptionsValidationError
// false -- err is instanceof Error but not WebpackOptionsValidationError

transpiled:

__extends(WebpackOptionsValidationError, _super);
function WebpackOptionsValidationError() {
    var _this = _super.call(this) || this;
    Error.captureStackTrace(_this, WebpackOptionsValidationError);
    _this.name = 'WebpackOptionsValidationError';
    return _this;
    // here return instance of Error,
    // it refers to the prototype object of WebpackOptionsValidationError
}

The wrong behavior is result from #11846 and #11868.

@e-cloud
Copy link
Contributor Author

e-cloud commented Nov 9, 2016

@vladima @mhegazy @falsandtru

@falsandtru
Copy link
Contributor

See #10166

@e-cloud
Copy link
Contributor Author

e-cloud commented Nov 9, 2016

@falsandtru #10166 is related but not the solution.

I tried other class inheritances and it turns out the problem is not particular to Error inheritance.

see another example: (With Typescript 2.1 latest)

class CustomObject extends Object {
    constructor(message) {
        super()
        this.message = message
    }
}

const obj = new CustomObject('a')
console.log(obj instanceof CustomObject)

Expected output:

true

Actual:

false

You can try a playground demo(with Typescript 2.0.x)
Hit the run button and the console will log true which is expected

Conclusion:
this may breaks some code widely with Typescript 2.1


PS: One interesting thing is Babel also transpile it incorrectly

@falsandtru
Copy link
Contributor

I wrote my solution in that issue.

@e-cloud
Copy link
Contributor Author

e-cloud commented Nov 9, 2016

compromising solution in #12123

@e-cloud
Copy link
Contributor Author

e-cloud commented Dec 9, 2016

Due to design limitation, close this

@e-cloud e-cloud closed this as completed Dec 9, 2016
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 12, 2016
@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants