Skip to main content
The + operator in TypeScript functions as both a unary numeric coercion operator and a binary operator for numeric addition or string concatenation. TypeScript applies strict static type-checking rules to its operands, preventing many of the implicit type coercions permitted in standard JavaScript to enforce type safety.

Unary + Operator

The unary + operator precedes a single operand and evaluates it as a number. Syntax:
Type Rules:
  • Valid Operands: TypeScript strictly limits the unary + operator to operands of type number, string, any, and numeric enums.
  • Return Type: Always resolves to the number type at compile time.
  • Compiler Restrictions: TypeScript explicitly forbids applying the unary + to types such as boolean, null, undefined, bigint, or symbol. Attempting to do so results in a compile-time error.

Binary + Operator

The binary + operator sits between two operands, performing either mathematical addition or string concatenation based on the resolved types of the operands. Syntax:
Type Rules & Resolution: TypeScript evaluates the binary + using the following strict type-checking hierarchy:
  1. String Concatenation: If either operand is of type string, the operation is treated as concatenation. The non-string operand is implicitly coerced, and the return type is string.
  1. Numeric Addition: If both operands are of type number (or numeric enums), the operation performs mathematical addition. The return type is number.
  1. BigInt Addition: If both operands are of type bigint, the operation performs BigInt addition. The return type is bigint.
Compiler Restrictions: Unlike JavaScript, TypeScript rejects binary + operations between incompatible types.
  • Mixed Numerics: You cannot mix number and bigint.
  • Invalid Primitives: You cannot add boolean, null, or undefined to a number or to each other.
  • Objects/Arrays: You cannot use the + operator on objects or arrays unless one operand is a string or the types are explicitly cast to any.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More