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 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.
- TypeScript permits the unary
+operator onstring,number,any, andenumtypes. - If the operand is a
stringthat cannot be parsed as a number, it evaluates toNaNat runtime, but TypeScript still statically infers the resulting type asnumber. - TypeScript strictly rejects the unary
+operator onunknown,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.
- Numeric Addition: If both operands are of type
number, the operation performs arithmetic addition and returns anumber. - BigInt Addition: If both operands are of type
bigint, the operation performs arithmetic addition and returns abigint. - String Concatenation: If at least one operand is of type
string, the operation performs string concatenation and returns astring. However, the non-string operand must be a valid primitive (e.g.,number,bigint,boolean,null,undefined) orany. TypeScript will reject the operation if the non-string operand is an object or array. - Type Incompatibility: TypeScript rejects binary
+operations between incompatible primitive types (e.g.,numberandboolean, ornumberandbigint). - Object Rejection: TypeScript strictly rejects objects as operands for the binary
+operator (unless typed asany). It does not perform structural checks forvalueOf()ortoString()methods; developers must explicitly invoke these methods to extract a primitive value before applying the operator.
Master TypeScript with Deep Grasping Methodology!Learn More





