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
I got hit by the same problem.
This is an intended change.
This is documented here and very interesting, relevant discussions can be found in #12123 #12581 among others.
@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.
@dbaeumer great point. That would definitely be good.
I had this issue and I followed the recommendation in the linked related issues to remove extends Error. Seems to work.
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) :)
@jamesrichford +1 for initiative with the new package. -10 million for forking the web! 😆
@aluanhaddad haha how do you mean breaking the web?
I meant to write "forking the web" but my brain isn't working at all today...
@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.
@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 :)
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.
Most helpful comment
This is documented here and very interesting, relevant discussions can be found in #12123 #12581 among others.