类型"ObjectConstructor"上不存在属性"setPrototypeOf" [英] Property 'setPrototypeOf' does not exist on type 'ObjectConstructor'

查看:292
本文介绍了类型"ObjectConstructor"上不存在属性"setPrototypeOf"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to implement polyfill Object.setPrototypeOf as is descrbed in:

https://developer.mozilla .org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf

这是我的polyfill.ts:

It is my 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:

polyfills.d.ts:

declare interface ObjectConstructor{

        setPrototypeOf(obj: any, proto: any);

}

我尝试了polyfill.d.ts的许多可能性:

I was trying with many possibilities of polyfill.d.ts:

declare global {

    export interface ObjectConstructor {
        setPrototypeOf(obj: any, proto: any);
    }
}

仍然出现以下错误:

[ts]属性'setPrototypeOf'在类型上不存在 'ObjectConstructor'.您是说'getPrototypeOf'吗? lib.es5.d.ts(164, 5):在此处声明了"getPrototypeOf".

[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的原因.

I migrate quite large application to TypeScript 3.0.3 that is why I need to implement the polyfill.

找到的解决方案:

删除polyfill和:

It is good enough to delete polyfill and:

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

更多: Typescript-扩展错误类

推荐答案

我如何应对这种(不理想的)解决方案:

The way how I coped with that (not ideal) solution:

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

这篇关于类型"ObjectConstructor"上不存在属性"setPrototypeOf"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆