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

The `-` operator in TypeScript functions in two distinct contexts: at the type level as a mapping modifier to remove property constraints, and at runtime as a standard arithmetic subtraction and unary negation operator subject to strict static type checking.

## Type-Level Semantics: Mapping Modifiers

In TypeScript's type system, the `-` operator is used exclusively within mapped types to subtract (remove) specific modifiers from properties during type transformations. It can be applied to the `readonly` and `?` (optional) modifiers.

**Syntax:**

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

* **`-readonly`**: Strips the `readonly` modifier from the property key, making the resulting property mutable.
* **`-?`**: Strips the `?` modifier from the property key, making the resulting property strictly required and removing `undefined` from the property's union type (unless `undefined` is explicitly defined in the base type).

**Type Evaluation:**

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

// The compiler evaluates this by stripping both 'readonly' and '?'
type ProcessedType = {
    -readonly [K in keyof SourceType]-?: SourceType[K];
};

// Resulting type equivalent:
// type ProcessedType = {
//     id: number;
//     name: string;
// }
```

## Runtime Semantics: Arithmetic and Unary Negation

At runtime, TypeScript inherits JavaScript's `-` operator behavior but applies strict static type checking to the operands to prevent unintended implicit type coercion.

**Binary Subtraction (`A - B`)**
The compiler requires both operands to be of type `number`, `bigint`, `any`, or a numeric `enum`. TypeScript will throw a compilation error if the operands are of incompatible types or if you attempt to mix `number` and `bigint`.

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

const bigA: bigint = 10n;
const bigB: bigint = 5n;
const resultBig: bigint = bigA - bigB; // Valid

enum Status { Pending = 1, Active = 2 }
const resultEnum: number = Status.Active - Status.Pending; // Valid

// Compiler Error ts(2365): Operator '-' cannot be applied to types 'number' and 'bigint'.
const invalidMixed = numA - bigA; 

// Compiler Error ts(2362): The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
const invalidString = "10" - 5; 
```

**Unary Negation (`-A`)**
When used as a unary prefix, the `-` operator converts the operand into a numeric value and negates it. TypeScript enforces that the operand must be of a type mathematically compatible with negation (`number`, `bigint`, `any`, or a numeric `enum`).

```typescript theme={"dark"}
const positive: number = 42;
const negative: number = -positive; // Evaluates to -42

const bigVal: bigint = 42n;
const negBig: bigint = -bigVal; // Evaluates to -42n

enum Offset { Base = 5 }
const negEnum: number = -Offset.Base; // Evaluates to -5

// Compiler Error ts(2356): An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.
const invalidUnary = -"hello";
```

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