Skip to main content
The + operator in TypeScript functions as both a unary numeric coercion operator and a binary operator for arithmetic addition and string concatenation. TypeScript applies strict static type-checking rules to its operands, restricting the implicit type coercion permitted in standard JavaScript to ensure type safety at compile time.

Unary + Operator

The unary + operator precedes a single operand and instructs the runtime to coerce the operand into a numeric value. In TypeScript, applying this operator results in an expression statically typed as number.
Type Behavior and Constraints:
  • TypeScript permits the unary + operator on string, number, any, and enum types.
  • If the operand is a string that cannot be parsed as a number, it evaluates to NaN at runtime, but TypeScript still statically infers the resulting type as number.
  • TypeScript strictly rejects the unary + operator on unknown, boolean, bigint, and object types, throwing compile-time errors.

Binary + Operator

The binary + operator sits between two operands. TypeScript evaluates the static types of both the left and right operands to determine whether to perform arithmetic addition or string concatenation, strictly limiting which type combinations are legally permissible.
Type Behavior and Constraints:
  1. Numeric Addition: If both operands are of type number, the operation performs arithmetic addition and returns a number.
  2. BigInt Addition: If both operands are of type bigint, the operation performs arithmetic addition and returns a bigint.
  3. String Concatenation: If at least one operand is of type string, the operation performs string concatenation and returns a string. However, the non-string operand must be a valid primitive (e.g., number, bigint, boolean, null, undefined) or any. TypeScript will reject the operation if the non-string operand is an object or array.
  4. Type Incompatibility: TypeScript rejects binary + operations between incompatible primitive types (e.g., number and boolean, or number and bigint).
  5. Object Rejection: TypeScript strictly rejects objects as operands for the binary + operator (unless typed as any). It does not perform structural checks for valueOf() or toString() methods; developers must explicitly invoke these methods to extract a primitive value before applying the operator.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More