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

TypeScript 2.1.x fails to extend Error #12790

Closed
jamesadarich opened this issue Dec 9, 2016 · 13 comments
Closed

TypeScript 2.1.x fails to extend Error #12790

jamesadarich opened this issue Dec 9, 2016 · 13 comments
Labels
Breaking Change Would introduce errors in existing code Duplicate An existing issue was already created

Comments

@jamesadarich
Copy link

jamesadarich commented Dec 9, 2016

TypeScript Version: 2.1.4

Code

Given the following definition

export default class ExtendedError extends Error {
    public get something(): number {
        return 42;
    }

    public doSomething() {

    }

}

I get the following output for 2.0.3

const extendedErrorInstance = new ExtendedError();

console.log("Is instanceof ExtendedError:", extendedErrorInstance instanceof ExtendedError);
// outputs "Is instanceof ExtendedError: true"
console.log("Is instanceof Error:", extendedErrorInstance instanceof Error);
// outputs "Is instanceof Error: true"
console.log("Value of something:", extendedErrorInstance.something);
// outputs "Value of something: 42"
console.log("Do something:", extendedErrorInstance.doSomething());
// executes function and outputs "Do something: undefined"

However for version 2.1.1 and 2.1.4 I get the following

const extendedErrorInstance = new ExtendedError();

console.log("Is instanceof ExtendedError:", extendedErrorInstance instanceof ExtendedError);
// outputs "Is instanceof ExtendedError: false"
console.log("Is instanceof Error:", extendedErrorInstance instanceof Error);
// outputs "Is instanceof Error: true"
console.log("Value of something:", extendedErrorInstance.something);
// outputs "Value of something: undefined"
console.log("Do something:", extendedErrorInstance.doSomething());
// throws error "TypeError: extendedErrorInstance.doSomething is not a function"

Expected behavior:
The extended error to behave as in the first example

Actual behavior:
It seems as if the error has not been extended at all

Thanks very much for your assistance in advance.

James

@dbaeumer
Copy link
Member

dbaeumer commented Dec 9, 2016

I got hit by the same problem.

@aluanhaddad
Copy link
Contributor

This is an intended change.

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Dec 9, 2016

This is documented here and very interesting, relevant discussions can be found in #12123 #12581 among others.

@dbaeumer
Copy link
Member

dbaeumer commented Dec 9, 2016

@aluanhaddad thanks for the info. Would be cool to link this here as well: http://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html section Use returned values from super calls as ‘this’. I read this and having a pointer to the other doc would have saved quite some time.

@aluanhaddad
Copy link
Contributor

@dbaeumer great point. That would definitely be good.

@andriijas
Copy link

I had this issue and I followed the recommendation in the linked related issues to remove extends Error. Seems to work.

@mhegazy mhegazy added Duplicate An existing issue was already created Breaking Change Would introduce errors in existing code labels Dec 9, 2016
@jamesadarich
Copy link
Author

Ace, good to have the link because I missed too :)

@andriijas I've just made a small npm module to allow you to extend it adds a little easier and also so you don't have to set the prototype for errors that extend errors etc. (so handles the chaining for you) :)

https://www.npmjs.com/package/extendo-error

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Dec 10, 2016

@jamesrichford +1 for initiative with the new package. -10 million for forking the web! 😆

@jamesadarich
Copy link
Author

@aluanhaddad haha how do you mean breaking the web?

@aluanhaddad
Copy link
Contributor

I meant to write "forking the web" but my brain isn't working at all today...

@blakeembrey
Copy link
Contributor

@jamesrichford I would recommend using any of the existing packages on NPM today instead of making a new one. For instance, I use https://www.npmjs.com/package/make-error, but there's a few others.

One note on your code, I would do https://github.com/jamesrichford/extendo-error/blob/master/src/extendo-error.ts#L17 outside the constructor and not inside since it's really the same operation every time.

@jamesadarich
Copy link
Author

@blakeembrey absolutely agree usually would but this package didn't come up on my searches thanks for pointing it out - will be useful for the future. Also thanks for the mini code review :)

@blakeembrey
Copy link
Contributor

All good, finding quality modules on NPM can be tricky :) Especially for this, took me a while and various code reviews to decide I'd use and build with it going forward.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Breaking Change Would introduce errors in existing code Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

7 participants