属性 'setPrototypeOf' 在类型 'ObjectConstructor' 上不存在

Property 'setPrototypeOf' does not exist on type 'ObjectConstructor'

我想实现 polyfill Object.setPrototypeOf,如下所述:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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] Property 'setPrototypeOf' does not exist on type 'ObjectConstructor'. Did you mean 'getPrototypeOf'? lib.es5.d.ts(164, 5): 'getPrototypeOf' is declared here.

我将相当大的应用程序迁移到 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)
    }
}

更多:

添加到 tsconfig.json 这个:

{
  "compilerOptions": {
    "lib": [
      ...
      "es7"
    ]
  }
}

我处理那个(不理想)解决方案的方式:

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)
    }
}