-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Wrong constructor of Error subclass from 2.1.1 #12660
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
Comments
A summary of what's going on based on a conversation with @rbuckton:
Possible fixes: Either don't use
class Foo extends Error {
constructor() {
super("oh no");
}
}
let x = new Foo();
// Expected: x.message === "oh no"
// Actual: x.message === ""
alert(x.message); |
My recommendation to get this working is to set the prototype explicitly on your instances. for instance: class MyError extends Error {
sayHello() {
return "hello " + this.message;
}
constructor(m) {
super(m);
// Set the prototype explictilly
Object.setPrototypeOf(this, MyError.prototype);
}
}
var e = new MyError("my error");
console.log("e instanceof Error => " + (e instanceof Error)); // true
console.log("e instanceof MyError => " + (e instanceof MyError)); // true
console.log("e.message => " + (e.message)); // "my error"
console.log("e.sayHello() => " + (e.sayHello())); // "hello my error" The only change here from extending non-built-in classes is alternatively, you can just set |
Uh oh!
There was an error while loading. Please reload this page.
TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)
Code
Expected behavior:
e instanceof MyError === true;
Actual behavior:
e instanceof MyError === false
Note: it was working for ts 2.0.10, but not for ts 2.1.1
The text was updated successfully, but these errors were encountered: