-
Notifications
You must be signed in to change notification settings - Fork 12.8k
The type of bitwise operations on 'any' operands should be 'number | bigint' #42125
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
Comments
That wouldn't make your example 2 valid. You could still call |
@MartinJohns You must be right. I fixed my sample code, thanks! |
You really didn't, because you can't. There's no way to currently type this in a safe way, as mentioned in #12410 already. You would need #27808 for this.
|
In that case, I'll leave that part out of my example. Why am I using TypeScript anyway? |
Related: #41741 (possibly a duplicate) |
Clearly TypeScript thinks that certain operators can produce nothing but a number, which used to be true (i.e. |
The issue I last linked was for TypeScript 4.2.0, From the TypeScript Design Goals:
I would guess that in the majority of cases |
Well, I wasn't talking about the state of the dev version of TypeScript, but the current version of ECMAScript, or ES2020, where bigints are introduced as part of the language (JavaScript, not TypeScript). Certain expressions that could previously only produce number values now produce bigint values. It is well possible that the current TypeScript behavior with respect to the typing of such expressions strives to enhance productivity at the expenses of correctness. I would say though that legacy compatibility also needs some consideration here, as changing the current behavior would break existing code. |
Importantly, this statement is false insofar as it includes It seems like #41741 intended to suggest what you are suggesting, but incorrectly asserted that an expression like Itβs possible that the incorrect formulation of that issue caused it to be dismissed without really considering the suggestion here. I tend to agree with @MartinJohns that this would be a huge pain, and the existing workarounds of βavoid direct unchecked manipulation of |
I would also say that maybe you shouldnβt be allowed to use declare let a: never;
const x = a + a; // number, no error
const y = "a" + a; // no error |
@andrewbranch you are correct, I removed the mention of |
Here is a relevant recent example: // This works
type Bitmask = number | bigint;
export function toggleAllBits(bitmask: any): Bitmask {
const oneBit = (typeof bitmask === "bigint" ? 1n : 1) as typeof bitmask;
let start = oneBit;
while(start <= bitmask) {
bitmask ^= start;
start = start << oneBit;
}
return bitmask;
}
// Change the function's signature to:
export function toggleAllBits(bitmask: Bitmask): Bitmask {
// throws TS2365: Operator '^=' cannot be applied to types 'number | bigint' and 'number | bigint'. |
Bug Report
π Search Terms
π Version & Regression Information
β― Playground Link
Workbench Repro
π» Code
π Actual behavior
The result of the bitwise operations
&
,|
,^
and~
is assumed to be anumber
when all the operands have unspecified type, likeany
ornever
.π Expected behavior
The result of bitwise operations with unspecified type operands should be
number | bigint
.The text was updated successfully, but these errors were encountered: