You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Wrong constructor of Error subclass from 2.1.1 #12660
// A *self-contained* demonstration of the problem follows...classMyErrorextendsError{publiccode: number;constructor(code: number,message: string){super(message);this.code=code;}}conste=newMyError(0,"foo");
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
This didn't "work" in any meaningful sense in 2.0 -- the base constructor didn't properly run due to how weird internal objects like Error get constructed. See footnote*
This isn't possible to make work in regular ES5 because you need to do some prototype/proto hackery that can't be emulated without ES6 APIs
Targeting ES6 this is fine because you'll just have native class emit anyway
Possible fixes: Either don't use instanceof with custom classes, don't try to extend weird built-ins, or target ES6.
Activity
[-]Wrong subclass constructor from 2.1.1[/-][+]Wrong constructor of Error subclass from 2.1.1[/+]HerringtonDarkholme commentedon Dec 5, 2016
#12123
RyanCavanaugh commentedon Dec 5, 2016
A summary of what's going on based on a conversation with Ron Buckton (@rbuckton):
Errorget constructed. See footnote*Possible fixes: Either don't use
instanceofwith custom classes, don't try to extend weird built-ins, or target ES6.*Code:See also http://stackoverflow.com/a/17891099/1704166
mhegazy commentedon Dec 5, 2016
My recommendation to get this working is to set the prototype explicitly on your instances. for instance:
The only change here from extending non-built-in classes is
Object.setPrototypeOf(this, MyError.prototype);alternatively, you can just set
this.__proto__instead of using Object.setPrototypeOf`Yaojian commentedon Jun 6, 2018
With support for stack trace