question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. Itย collects links to all the places you might be looking at while hunting down a tough bug.

And, if youโ€™re still stuck at the end, weโ€™re happy to hop on a call to see how we can help out.

The type of bitwise operations on 'any' operands should be 'number | bigint'

See original GitHub issue

Bug Report

๐Ÿ”Ž Search Terms

  • bigint
  • bitwise

๐Ÿ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about bigint.

โฏ Playground Link

Workbench Repro

๐Ÿ’ป Code

//@target: ES2020
const v1 = 1n as any;
const v2 = 2n as never;
const v3 = v1 | v2; // v3 should be typed (number | bigint), but is number

๐Ÿ™ Actual behavior

The result of the bitwise operations &, |, ^ and ~ is assumed to be a number when all the operands have unspecified type, like any or never.

๐Ÿ™‚ Expected behavior

The result of bitwise operations with unspecified type operands should be number | bigint.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
fasttimecommented, Dec 27, 2020

Clearly TypeScript thinks that certain operators can produce nothing but a number, which used to be true (i.e. -value), but no longer holds in times of bigint.

0reactions
serbanghitacommented, Aug 21, 2022

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'.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Bitwise AND (&) - JavaScript - MDN Web Docs
The bitwise AND (&) operator returns a 1 in each bit position for which the corresponding bits of both operands are 1s.
Read more >
INT13-C. Use bitwise operators only on unsigned operands
Bitwise operators should be used only with unsigned integer operands, as the results of bitwise operations on signed integers areย ...
Read more >
Bitwise operators (Transact-SQL) - SQL Server - Microsoft Learn
Bitwise operators perform bit manipulations between two expressions of any of the data types of the integer data type category.
Read more >
BigInt - Grain Docs
Functions for converting between Numbers and the BigInt type. ... Converts a Number to a BigInt. ... Returns the absolute value of the...
Read more >
Expression types - IBM
Each of the bitwise operators and (&) , or (|), and exclusive or (^) perform a bitwise operation on two operands, returning a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found