Skip to main content
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:
  • -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:

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.
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).
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More