Skip to main content

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.

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:
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:
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.
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).
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";
Master TypeScript with Deep Grasping Methodology!Learn More