Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use bigint with any in certain arithmetic expressions #41741

Closed
abacabadabacaba opened this issue Dec 1, 2020 · 6 comments
Closed

Cannot use bigint with any in certain arithmetic expressions #41741

abacabadabacaba opened this issue Dec 1, 2020 · 6 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@abacabadabacaba
Copy link

TypeScript Version: 4.2.0-dev.20201130

Search Terms: is:issue bigint

Code

var x: any, y: bigint
-x + y
++x + y
x++ + y
~x + y
x - x + y
x * x + y

Expected behavior:

The code should compile without errors, because x could be a bigint.

Actual behavior:

Each of the expressions fails to typecheck: error TS2365: Operator '+' cannot be applied to types 'number' and 'bigint'..

Playground Link: link

Related Issues: none.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 1, 2020
@RyanCavanaugh
Copy link
Member

This was intentional since, unlike other operands, operations like 80n + 1 throw rather than produce possibly-useful values.

@abacabadabacaba
Copy link
Author

@RyanCavanaugh I disagree. Since x is any, it could be a bigint, in which case all of the expressions are perfectly valid. The problem is that if the type of x is any, the type of -x, x - x, etc. is deduced to be number, which is wrong because it could be bigint.

@abacabadabacaba
Copy link
Author

Another example:

var x: any = 0n
var y = -x

Now, the type of y is number, but the value is 0n, a bigint.

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@prettydiff
Copy link

prettydiff commented Jan 13, 2021

I am getting this error: Operator '+' cannot be applied to types 'BigInt' and 'BigInt'

TypeScript version 4.1.3.

I can understand throwing an error for applying arithmetic to mixed types since BigInt and number are not compatible, but this is a BigInt plus a BigInt. For some background the operation is question is:

details.size = details.size + size;
// details.size is defined as a BigInt
// size comes from "size" property of Node type BigIntStats

EDIT: The problem is that @types/node was out of date. I updated to version 14.14.20 and it reports BigIntStats.size as type bigint all lowercase which is not compatible with language standard type BigInt.

EDIT2: Seems I needed to read the documentation:
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-2.html#bigint
BigInt support in TypeScript introduces a new primitive type called the bigint (all lowercase).

@fwienber
Copy link

Another example:

var x: any = 0n
var y = -x

Now, the type of y is number, but the value is 0n, a bigint.

This issue has been marked 'Working as Intended' ...

I can understand the design decision to not allow "potentially mixed computations", but this last example is clearly wrong typing, they can't be serious this is "working as intended". The original argument was to "throw rather than produce possibly-useful values" (@RyanCavanaugh), but this example shows that there are cases where the typing is too "optimistic".
It seems the unary - operator suffers the same problem as update expressions (++, --), see #49558. I could include your example in that issue as it seems to be a closely related problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants