Using BigInt in TypeScript

BigInt support has been added on TypeScript 3.2; make sure your version is compatible.

But on top of that, you need to decide how BigInt will be supported on the context of your script - will you provide polyfills, or will you only run your script on environments that are guaranteed to have BigInt support?

This means you will need esnext as your build target (likely on tsconfig.json's target field), since BigInt is not compatible with previous ECMAScript versions.

If you do include a BigInt polyfill, you can use esnext.bigint as part of the lib field during transpilation. This adds the needed definitions to the process.


In my case, I should be add "node" to types in my tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "outDir": "build/module",
    "module": "esnext",
    "types": ["node"]
  },
  "exclude": [
    "node_modules/**"
  ]
}

It works for me to add esnext.bigint in lib .

{
  "compilerOptions": {
    "lib": ["es6","esnext.bigint"]
  }
}

Tags:

Typescript