Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The |= (bitwise OR assignment) operator performs a bitwise OR operation between the binary representations of its left and right operands, assigning the resulting value to the left operand.

Syntax

x |= y;
This is syntactically equivalent to:
x = x | y;

Mechanics

  1. Operand Conversion:
    • For number types, TypeScript (following JavaScript semantics) implicitly truncates both operands into 32-bit signed integers represented in two’s complement format.
    • For bigint types, operands are not truncated to 32 bits and retain their arbitrary precision during the bitwise operation.
  2. Bitwise Evaluation: The operator compares the operands bit by bit. If either the left bit or the right bit at a given position is 1, the corresponding bit in the result is set to 1. It yields 0 only if both bits are 0.
  3. Assignment: The evaluated result is assigned back to the left operand. For number types, the 32-bit integer result is converted back into a standard IEEE 754 double-precision float before assignment.

Truth Table

| Bit x | Bit y | Result (x | y) | | :---: | :---: | :---: | | 0 | 0 | 0 | | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 1 |

Code Visualization

let a: number = 5;  
// 32-bit binary: 00000000000000000000000000000101

let b: number = 3;  
// 32-bit binary: 00000000000000000000000000000011

a |= b;             
// Bitwise OR:    00000000000000000000000000000111
// Decimal value: 7

console.log(a); // 7

TypeScript Type Constraints

TypeScript enforces strict type checking on bitwise operators to prevent unintended runtime coercion:
  • Valid Numeric Types: Operands must be of a valid numeric type (number, bigint, or an enum). Attempting to use |= with strictly typed non-numeric types (such as string, boolean, or object) will trigger compiler error TS2362 (for the left-hand side) or TS2363 (for the right-hand side), stating that the operand “must be of type ‘any’, ‘number’, ‘bigint’ or an enum type.” Variables explicitly typed as any will bypass this check and will not throw an error.
  • Type Homogeneity: TypeScript requires both operands to be of the exact same type. You cannot mix number and bigint without explicit casting. Mixing incompatible but otherwise valid numeric types triggers TS2365.
let num: number = 10;
num |= 2; // Valid

let big: bigint = 10n;
big |= 2n; // Valid

// Mixing types:
// big |= 2; 
// Error TS2365: Operator '|=' cannot be applied to types 'bigint' and 'number'.

// Invalid types:
let str: string = "2";
// num |= str; 
// Error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
Master TypeScript with Deep Grasping Methodology!Learn More