typescript - 在 TypeScript 中使用 BigInt

标签 typescript

我相信我现在应该能够在 TypeScript 中使用 BigInt ……但是更改了一些代码库来使用它,我得到“找不到名称‘BigInt’。(2304)”关于为什么我会收到这个错误的任何想法?

$ cat test.ts 
let x = BigInt(123)
console.log(x.toString())

$ tsc test.ts 
test.ts:1:9 - error TS2304: Cannot find name 'BigInt'.

1 let x = BigInt(123)
          ~~~~~~


Found 1 error.

我现在如何在 TypeScript 中开始使用 BigInt?

最佳答案

BigInt已在 TypeScript 3.2 上添加支持;确保您的版本兼容。

但最重要的是,您需要决定如何BigInt将在您的脚本上下文中得到支持 - 您会提供 polyfill,还是仅在保证具有 BigInt 的环境中运行您的脚本支持?

这意味着 you will need esnext 作为您的构建目标(可能在 tsconfig.jsontarget 字段上),因为 BigInt与以前的 ECMAScript 版本不兼容。

如果您确实包含 BigInt polyfill,你可以使用 esnext.bigint作为 lib 的一部分转译期间的场。这会将所需的定义添加到流程中。

关于typescript - 在 TypeScript 中使用 BigInt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59456390/

相关文章:

typescript - gulp-typescript 和 typescript 的新@types 打字?

typescript - typescript 无法理解通用 typescript 函数中的返回类型

javascript - 箭头函数和bind()的区别

typescript - 有没有办法强制 Set<T> 构造函数的调用者提供类型参数?

jquery - 滑动/滑动标签 ionic 2

typescript - angular 2 从父组件访问子组件属性

javascript - Angular 'values is undefined' 订阅映射的 http 响应(未发出请求?)

typescript - 数值在 firestore 中保存为字符串

JQuery async .each 返回空的 promise

typescript - 在 TypeScript 中切换特定类型