我想实现polyfill Object.setPrototypeOf,如下所述:
这是我的polyfill.ts:
// Only works in Chrome and FireFox, does not work in IE:
Object.setPrototypeOf = Object.setPrototypeOf || function(obj, proto) {
obj.__proto__ = proto;
return obj;
}
polyfills.d.ts:
declare interface ObjectConstructor{
setPrototypeOf(obj: any, proto: any);
}
我尝试了polyfill.d.ts的许多可能性:
declare global {
export interface ObjectConstructor {
setPrototypeOf(obj: any, proto: any);
}
}
仍然出现以下错误:
[ts]属性'setPrototypeOf'在类型上不存在 'ObjectConstructor'。您是说'getPrototypeOf'吗? lib.es5.d.ts(164, 5):在此处声明了“ getPrototypeOf”。
我将相当大的应用程序迁移到TypeScript 3.0.3,这就是为什么我需要实现polyfill的原因。
找到的解决方案:
删除polyfill和:
export class KnownError extends Error {
public isKnownError: boolean = true;
constructor(message: string) {
super(message);
this.message = message;
//good enough solution, transpiled to ES5
(<any>Object).setPrototypeOf(this, KnownError.prototype)
}
}
答案 0 :(得分:0)
将以下内容添加到tsconfig.json中:
{
"compilerOptions": {
"lib": [
...
"es7"
]
}
}
答案 1 :(得分:0)
我如何处理(不理想的)解决方案:
export class KnownError extends Error {
public isKnownError: boolean = true;
constructor(message: string) {
super(message);
this.message = message;
//good enough solution, transpiled to ES5
(<any>Object).setPrototypeOf(this, KnownError.prototype)
}
}