javascripttypescriptbigintecmascript-2020

What is the difference between 'bigint' (lowercase) and 'BigInt'?


So I am trying to update some typescript code that uses external library for big numbers to BigInt (ES2020) and linter is complaining a lot. I don't really understand what is going on here.

enter image description here

It looks like there are two different types - 'bigint' and 'BigInt' and this two are not compatible with each other.

when I change partialValue type to 'bigint' the error disappears. Does this mean I should use 'bigint' instead of 'BigInt'? It is 'BigInt' in the documentation. Thats pretty confusing I must say.


Solution

  • bigint' (lowercase) is a built-in primitive type introduced in ECMAScript 2020 (ES2020) to represent arbitrary precision integers. It is used to work with large integers that cannot be accurately represented using the regular 'number' type.

    On the other hand, 'BigInt' (uppercase) is the constructor function for creating 'bigint' instances. It is part of the global namespace and allows you to create 'bigint' values using the 'BigInt()' function.

    The confusion arises because the 'bigint' primitive type shares the same name with the 'BigInt' constructor function. However, they are not the same thing and cannot be used interchangeably.

    In TypeScript, when you use the lowercase 'bigint' as a type annotation, you are explicitly specifying that a variable should be of type 'bigint'. For example:

    let myNumber: bigint = BigInt(12345);
    

    In the above code, 'myNumber' is explicitly declared as a 'bigint' using the lowercase type annotation.

    When you use the uppercase 'BigInt', it refers to the constructor function and not the primitive type. If you try to use 'BigInt' as a type annotation, TypeScript treats it as a different type that is not compatible with 'bigint'. This is why you're seeing errors when you change the type to 'BigInt'.