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 + 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:
+operand
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.
const str: string = "42";
const num: number = +str; // Valid: resolves to number

const bool: boolean = true;
const invalidBool: number = +bool; // Error TS2469: Operator '+' cannot be applied to type 'boolean'.

const big: bigint = 100n;
const invalidBig: number = +big; // Error TS2469: Operator '+' cannot be applied to type 'bigint'.

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:
leftOperand + rightOperand
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.
const a: string = "Value: " + 42; // Resolves to string
const b: string = true + " is boolean"; // Resolves to string
  1. Numeric Addition: If both operands are of type number (or numeric enums), the operation performs mathematical addition. The return type is number.
const c: number = 10 + 5; // Resolves to number
  1. BigInt Addition: If both operands are of type bigint, the operation performs BigInt addition. The return type is bigint.
const d: bigint = 10n + 20n; // Resolves to 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.
const err1 = 5 + true; // Error TS2365: Operator '+' cannot be applied to types 'number' and 'boolean'.
const err2 = 10n + 5;  // Error TS2365: Operator '+' cannot be applied to types 'bigint' and 'number'.
const err3 = {} + {};  // Error TS2365: Operator '+' cannot be applied to types '{}' and '{}'.
Master TypeScript with Deep Grasping Methodology!Learn More