Skip to content

Unable to extend Error when targeting es5 with TS 2.1 #12058

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

Closed
asvetliakov opened this issue Nov 4, 2016 · 5 comments
Closed

Unable to extend Error when targeting es5 with TS 2.1 #12058

asvetliakov opened this issue Nov 4, 2016 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@asvetliakov
Copy link

asvetliakov commented Nov 4, 2016

TypeScript Version: 2.1.0-dev.20161104

Code
tsconfig.json

{                       
  "compilerOptions": {  
    "target": "es5",    
    "module": "commonjs"
  }      
}                       
// A *self-contained* demonstration of the problem follows...
class Test extends Error {
        constructor(message: string) {
                super(message);
        }
}

const test = new Test("test");

console.log(`Instance of error: ${test instanceof Error}`);
console.log(`Instance of test: ${test instanceof Test}`);

Expected behavior:

Instance of error: true
Instance of test: true

Actual behavior:

Instance of error: true
Instance of test: false

It works with TS 2.0.7

@asvetliakov asvetliakov changed the title Unable to extend error when targeting es5 Unable to extend Error when targeting es5 with TS 2.1 Nov 4, 2016
@aluanhaddad
Copy link
Contributor

For reference, this affects all code that extends a function that is both callable constructable. For example, see #11919. Since Error(msg) is a factory, its return value is propagated through the super call and used instead of this.

@asvetliakov
Copy link
Author

asvetliakov commented Nov 6, 2016

I won't complain if it's intentional change (workaround-ed easily), but at least it should be documented (i.e. breaking change) or even better the TS compiler should give error on es5 target when 2.1 will be released.

@aluanhaddad
Copy link
Contributor

@asvetliakov I agree. I don't know what the intent exactly is. Either way subclassing builtins like Array and Error is broken. An error should be issued.

@DanielRosenwasser
Copy link
Member

It actually is an intentional change and documented here. I apologize about the inconvenience.

@DanielRosenwasser
Copy link
Member

Marking as a duplicate of #12123.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Dec 6, 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