英文:
What is the difference between 'bigint' (lowercase) and 'BigInt'?
问题
I am trying to update some TypeScript code that uses an external library for big numbers to BigInt (ES2020), and the linter is complaining a lot. I don't really understand what is going on here.
It looks like there are two different types - 'bigint' and 'BigInt,' and these 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. That's pretty confusing, I must say.
英文:
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.
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.
答案1
得分: 1
'bigint'(小写)是在ECMAScript 2020(ES2020)中引入的内置原始类型,用于表示任意精度的整数。它用于处理无法使用常规的'number'类型准确表示的大整数。
另一方面,'BigInt'(大写)是用于创建'bigint'实例的构造函数。它是全局命名空间的一部分,允许您使用'BigInt()'函数创建'bigint'值。
混淆之处在于'bigint'原始类型与'BigInt'构造函数共享相同的名称。但是,它们不是同一种东西,不能互换使用。
在TypeScript中,当您使用小写的'bigint'作为类型注释时,您明确指定变量应该是'bigint'类型。例如:
let myNumber: bigint = BigInt(12345);
在上面的代码中,'myNumber'使用小写类型注释明确声明为'bigint'。
当您使用大写的'BigInt'时,它指的是构造函数,而不是原始类型。如果您尝试使用'BigInt'作为类型注释,TypeScript会将其视为与'bigint'不兼容的不同类型。这就是当您将类型更改为'BigInt'时出现错误的原因。
英文:
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'.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论