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

The `-` operator in TypeScript operates in two distinct compilation contexts: at the type level to declare negative literal types and remove mapping modifiers, and at the value level as a strictly-typed arithmetic operator for binary subtraction and unary negation.

## Type-Level Negative Literals

In TypeScript's type system, the `-` operator acts as a prefix to define negative numeric and `bigint` literal types. It modifies a positive literal type into its negative counterpart.

**Syntax:**

```typescript theme={"dark"}
type NegativeNumber = -42;
type NegativeBigInt = -10n;
```

## Type-Level Mapping Modifier

Within mapped types, the `-` operator is used as a prefix to subtract (remove) specific modifiers from property signatures. It can be applied to the `readonly` modifier to remove immutability, or to the `?` modifier to remove optionality.

**Syntax:**

```typescript theme={"dark"}
type RemoveModifiers<T> = {
  -readonly [P in keyof T]-?: T[P];
};
```

* `-readonly`: Strips the `readonly` flag, making the resulting property mutable.
* `-?`: Strips the `?` flag, making the resulting property required.

**Type Resolution Example:**

```typescript theme={"dark"}
interface SourceType {
    readonly id?: number;
    readonly name?: string;
}

// Applies the `-` operator to both modifiers
type ModifiedType = {
    -readonly [K in keyof SourceType]-?: SourceType[K];
};

// Resulting Type:
// type ModifiedType = {
//     id: number;
//     name: string;
// }
```

## Value-Level Binary Subtraction

At runtime, the `-` operator performs binary subtraction. Unlike JavaScript, which allows implicit type coercion (e.g., `"5" - 2`), TypeScript's static type checker enforces strict operand constraints. Both the left and right operands must resolve to `number`, `bigint`, `any`, or an `enum` type.

**Syntax:**

```typescript theme={"dark"}
let result = expressionA - expressionB;
```

**Type Checking Behavior:**

```typescript theme={"dark"}
const numA: number = 10;
const numB: number = 5;
const validSub: number = numA - numB; 

const bigA: bigint = 100n;
const bigB: bigint = 50n;
const validBigSub: bigint = bigA - bigB;

// Compiler Error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
const invalidSub = "10" - 5; 

// Compiler Error TS2365: Operator '-' cannot be applied to types 'number' and 'bigint'.
const mixedSub = numA - bigA; 
```

## Value-Level Unary Negation

When used as a unary prefix, the `-` operator negates the numeric value of its operand. TypeScript enforces strict type constraints, rejecting types that would rely on JavaScript's implicit runtime coercion to `NaN` or numbers.

**Syntax:**

```typescript theme={"dark"}
let negated = -expression;
```

**Type Checking Behavior:**

```typescript theme={"dark"}
const baseVal: number = 42;
const negatedVal: number = -baseVal; // Resolves to -42

const baseBig: bigint = 42n;
const negatedBig: bigint = -baseBig; // Resolves to -42n

// Compiler Error TS2356: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.
const invalidUnary = -"42";
const invalidBoolean = -true;
```

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