继承异常和instanceof
Inherited exceptions and instanceof
为什么这个代码 returns false
?
class MyException extends Error {
constructor(message: string) {
super(message);
}
}
const e = new MyException('blah');
console.log(e instanceof MyException); // returns 'false'
当我执行以下代码时没有发生:
class Base {
constructor(message: string) {
console.log(message);
}
}
class MyClass extends Base {
constructor(message: string) {
super(message);
}
}
const e = new MyClass('blah');
console.log(e instanceof MyClass); // returns 'true'
这是一个已知问题:
instanceof is broken when class extends Error type 相关
到具有 TypeScript 功能的 Polymer 标准支持。
建议的解决方法是:
- 创建中介 class
- 设置原型
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;
}
}
为什么这个代码 returns false
?
class MyException extends Error {
constructor(message: string) {
super(message);
}
}
const e = new MyException('blah');
console.log(e instanceof MyException); // returns 'false'
当我执行以下代码时没有发生:
class Base {
constructor(message: string) {
console.log(message);
}
}
class MyClass extends Base {
constructor(message: string) {
super(message);
}
}
const e = new MyClass('blah');
console.log(e instanceof MyClass); // returns 'true'
这是一个已知问题: instanceof is broken when class extends Error type 相关 到具有 TypeScript 功能的 Polymer 标准支持。
建议的解决方法是:
- 创建中介 class
- 设置原型
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;
}
}