Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Keywords and Declarations

The delete operator

Reference for the delete operator in TypeScript: removing properties, the optional-operand rule (TS2790), arrays, frozen objects and better alternatives.

delete removes an own property from an object at runtime and evaluates to a boolean. It does not free memory directly and it does not touch variables; it only changes the object's shape by dropping a key.

TypeScript adds one rule on top of JavaScript: with strictNullChecks on, the operand of delete must be an optional property (or one whose type includes undefined). Deleting a required property would make the object lie about its type, so the compiler refuses.

TypeScript
interface Options { name: string; debug?: boolean }
declare const options: Options;
delete options.debug; // allowed: debug is optional

Try it

TypeScript Deleting optional properties

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Arrays, frozen objects and Reflect

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
delete object.property
delete object["property"]
delete object[expression]
Part Description
Operand A property access. Plain identifiers (delete x) are a syntax error in strict mode, which every module is.
Result true if the property is gone afterwards (including when it never existed); false only in sloppy mode when the property is non-configurable. In strict mode a non-configurable property throws TypeError.

Compiler error for a required property:

TypeScript
interface User { id: number; nickname?: string }
declare const user: User;
delete user.id; // error TS2790: The operand of a 'delete' operator must be optional.

Usage notes

  • Make the property optional first. If the key is meant to disappear, model it as key?: T. With exactOptionalPropertyTypes the type must be written key?: T (not T | undefined) for delete to be the "correct" way to remove it.
  • Arrays get holes. delete arr[i] keeps length and leaves an empty slot. Use splice, filter or toSpliced to remove elements.
  • Use immutable copies when you can. Destructuring with rest, const { token, ...rest } = settings, produces a new object without the key and keeps the types honest, without any delete.
  • Performance. Deleting properties can push V8 objects into slower dictionary mode. For hot objects, set the field to undefined or build a new object; for dynamic key sets use a Map.
  • Frozen, sealed and non-configurable properties cannot be deleted: strict code throws TypeError, while Reflect.deleteProperty returns false.
  • Not for variables or functions. delete never removes let, const, var or function bindings.
  • Optional chaining: delete a?.b is valid and does nothing (returns true) when a is nullish.