stackblitz 打字稿项目不知道 BigInt

stackblitz typescript project doesn't know BigInt

当您在 Stackblitz it turns out that there is an issue with BigInt

上创建 typescript 项目时

const val = BigInt(10);

它不知道BigInt。尽管代码运行良好,但我仍然希望错误消失:)

因此,在上述情况下,我可以添加以下内容

declare function BigInt(inp: number | string);

成功了,错误消失了。但是当我添加更多代码时,像这样

declare function BigInt(inp: number | string);

function convert(inp: string): BigInt {
    return BigInt(inp);
}

const val = BigInt(10);

上面的declare不够

它抱怨 BigInt 引用了一个值并被用作类型。

我不确定我能做些什么来解决这个问题。有什么建议吗?

BigInt 不是打字稿的一部分,它是某些浏览器 javascript 的一部分,但例如 Safari 不支持它,这意味着没有 polyfill 的代码将不会不在那里工作。

要正确定义它,您需要声明它的接口、变量和构造函数。


declare interface BigInt {
  // here all magic you want to declare
  test: number
};

declare var BigInt: BigIntConstructor;

declare interface BigIntConstructor {
    new(value?: any): BigInt;
    (value?: any): BigInt;
    readonly prototype: BigInt;
};



function convert(inp: string): BigInt {
    return BigInt(inp);
}

const val = BigInt(10).test; // an example.