【问题标题】:stackblitz typescript project doesn't know BigIntstackblitz typescript 项目不知道 BigInt
【发布时间】:2020-08-21 09:10:16
【问题描述】:

当您在 Stackblitz 上创建 typescript 项目时,发现 BigInt 存在问题

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 引用了一个值并且被用作一个类型。

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

【问题讨论】:

    标签: typescript stackblitz


    【解决方案1】:

    BigInt 不是 typescript 的一部分,它是某些浏览器的 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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-02
      • 2018-12-15
      • 2019-02-21
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      相关资源
      最近更新 更多