-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
instanceof is broken when class extends Error type #13965
Comments
Unfortunately this is a change that we made to try to try to adopt a more standard-compliant emit so that we could enable Polymer to work with TypeScript. For background, was an intentional change in 2.2 (see #12123 and the section on our wiki), but is difficult to overcome through compilation. I believe there's some conversation in #12790 for workarounds. A workaround you can take now is create an intermediate class that you can extend from. export interface MyErrorStatic {
new (message?: string): RxError;
}
export interface MyError extends Error {}
export const MyError: MyErrorStatic = function MyError(this: Error, message: string) {
const err = Error.call(this, message);
this.message = message;
this.stack = err.stack;
return err;
} as any;
export class HttpError extends MyError {
// ...
} In TypeScript 2.2, you'll be able to set the prototype on your own. // Use this class to correct the prototype chain.
export class MyError extends Error {
__proto__: Error;
constructor(message?: string) {
const trueProto = new.target.prototype;
super(message);
// Alternatively use Object.setPrototypeOf if you have an ES6 environment.
this.__proto__ = trueProto;
}
} Sorry for the inconvenience of this. |
What's the issue this is a duplicate of? I think this is still unresolved. I'm trying to implement an error handler in angular 5, but without instanceof working it's difficult |
For anyone looking for a workaround that will let them unit test their custom error classes using |
Ah, I ran into this issue yesterday. Definitely consumed several hours before I found this issue. Anyone have a good solution for this yet? |
@cjam You can set the prototype yourself as noted here: https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work |
Was this fixed? |
Workaround, requires unique properties on each error: source: http://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards |
I ran into this today. Is this a bug? |
@DanielRosenwasser I think this should be re-opened; this feels seriously broken as-is. |
Locking due to unproductive/repetitive discussion |
TypeScript Version: 2.1.1
Expected behavior:
prints
true
.Actual behavior:
prints
false
.Note:
This happens only when a type extends
Error
.After removing
extends Error
from BaseError everything works as expected.Links
Test case
The text was updated successfully, but these errors were encountered: