TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
% operator in TypeScript is the arithmetic remainder operator. It evaluates to the remainder left over when a dividend (the left operand) is divided by a divisor (the right operand) using truncating division.
TypeScript Type Constraints
TypeScript enforces strict type checking on the% operator. Valid operands must be of type number, bigint, any, or a numeric enum.
- Type Homogeneity: You cannot mix
numberandbigintoperands. Doing so results in a compiler error. - No Implicit Coercion: Unlike loose JavaScript, TypeScript prevents implicit coercion of invalid types (such as strings, booleans, or objects). It throws a
ts(2362)error if an invalid type is placed on the left-hand side of the operator, and ats(2363)error if an invalid type is placed on the right-hand side.
Evaluation Mechanics and Sign Preservation
A critical mechanical detail of the% operator is that it is a remainder operator, not a strict mathematical modulo. The sign of the returned result always matches the sign of the dividend (the left operand), regardless of the sign of the divisor.
The underlying evaluation follows the equation: r = n - (d * trunc(n / d))
Floating-Point Operands
The% operator fully supports floating-point numbers. It does not coerce operands to integers before evaluation.
Edge Cases and Special Values
When dealing withnumber types, the % operator adheres to IEEE 754 rules for special values:
- Division by Zero: Evaluates to
NaN(Not-a-Number). It does not throw a runtime error or evaluate toInfinity. - Infinity: If the dividend is
Infinityor-Infinity, the result isNaN. If the divisor isInfinity, the result is the dividend.
bigint, division by zero (10n % 0n) will throw a RangeError: Division by zero at runtime, as bigint does not have a NaN concept.
Master TypeScript with Deep Grasping Methodology!Learn More





