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 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.
+operand
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.
// Valid Operations
const str: string = "42";
const num: number = +str; // Statically typed as number

const invalidStr: string = "foo";
const nanVal: number = +invalidStr; // Statically typed as number, runtime value is NaN

// Invalid Operations (Compile-time Errors)
const unk: unknown = "42";
const err1 = +unk; 
// TS Error: Object is of type 'unknown'.

const bool: boolean = true;
const err2 = +bool; 
// TS Error: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.

const big: bigint = 10n;
const err3 = +big; 
// TS Error: The '+' operator cannot be applied to type 'bigint'.

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.
leftOperand + rightOperand
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.
// Valid Operations
const numSum: number = 10 + 5;
const bigSum: bigint = 10n + 5n;
const strConcat1: string = "Value: " + 42; 
const strConcat2: string = "Active: " + true;

// Invalid Operations (Compile-time Errors)
const err4 = 5 + true; 
// TS Error: Operator '+' cannot be applied to types 'number' and 'boolean'.

const err5 = 10n + 5; 
// TS Error: Operator '+' cannot be applied to types 'bigint' and 'number'.

const err6 = "Data: " + {}; 
// TS Error: Operator '+' cannot be applied to types 'string' and '{}'.

const err7 = {} + []; 
// TS Error: Operator '+' cannot be applied to types '{}' and 'never[]'.

// Correcting object operations via explicit method calls
const obj = { valueOf: () => 5 };
const validObjSum: number = obj.valueOf() + 10; // Valid
Master TypeScript with Deep Grasping Methodology!Learn More