gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:14:44 28 4
gpt4 key购买 nike

我想实现 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)
}
}

更多 Typescript - Extending Error class

最佳答案

将此添加到 tsconfig.json 中:

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

关于typescript - 类型 'setPrototypeOf' 上不存在属性 'ObjectConstructor',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52402166/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com