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 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:
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:
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:
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:
let result = expressionA - expressionB;
Type Checking Behavior:
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:
let negated = -expression;
Type Checking Behavior:
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;
Master TypeScript with Deep Grasping Methodology!Learn More