> ## 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.

# TypeScript Unary Plus

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`.

```typescript theme={"dark"}
+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.

```typescript theme={"dark"}
// 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.

```typescript theme={"dark"}
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.

```typescript theme={"dark"}
// 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
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
