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

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:**

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

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

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

```typescript theme={"dark"}
const a: string = "Value: " + 42; // Resolves to string
const b: string = true + " is boolean"; // Resolves to string
```

2. **Numeric Addition:** If both operands are of type `number` (or numeric enums), the operation performs mathematical addition. The return type is `number`.

```typescript theme={"dark"}
const c: number = 10 + 5; // Resolves to number
```

3. **BigInt Addition:** If both operands are of type `bigint`, the operation performs BigInt addition. The return type is `bigint`.

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

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

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