Typescript BigInt 添加问题:不能混合 BigInt 和其他类型,使用显式转换

问题描述

我们在构建服务器上看到以下编译错误

    TypeError: Cannot mix BigInt and other types,use explicit conversions
 
      119 |       const one: bigint = BigInt(1);
      120 |       const oldNum: bigint = oldLabelNumber.currentValue;
    > 121 |       const localLabelNum: bigint = oldNum + one;

我已明确输入变量 oneoldNum 以尝试向类型检查器证明我正在执行 BigInt 加法但无济于事。

对于上下文,这就是代码试图强迫类型检查器相信我正在执行 bigint 操作..(最初它是一个简单的三元表达式)

    const oldLabelNumber = await fetchLabelNumber();
    let expectedLabelNumber: bigint;
    if (oldLabelNumber != null) {
      const one: bigint = BigInt(1);
      const oldNum: bigint = oldLabelNumber.currentValue;
      const localLabelNum: bigint = oldNum + one;
      expectedLabelNumber = localLabelNum;
    } else {
      expectedLabelNumber = ... redacted ...;
    }

我怀疑这与 oldLabelNumber 的流类型有关??尽管如果是这种情况,我希望在第 120 行看到 TypeError

oldLabelNumber 的类型为:

{ currentValue: bigint } | null

我只在我们的构建服务器上遇到这个问题。它在本地编译并运行良好(相同的 Node 版本并使用本地 node_modules/.bin 中的 tsc)

可能值得注意的是,这是通过 Jest 运行的测试代码

CI 服务器详细信息:

  • 节点版本:12.18.0
  • 打字稿:4.1.3
  • "@types/node": "^14.14.20"
  • TS 配置:
{
  "compilerOptions": {
    "target": "ES2019","module": "commonjs","outDir": "./dist","strict": true,"esModuleInterop": true,"skipLibCheck": true,"forceConsistentCasingInFileNames": true
  }
}
  • Jest 配置:
module.exports = {
  preset: 'ts-jest',testEnvironment: 'node',testRegex: 'smoke\\.spec\\.ts$',testPathIgnorePatterns: ['/node_modules/','/dist/'],testTimeout: 30000,};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)