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.
interface Options { name: string; debug?: boolean } declare const options: Options; delete options.debug; // allowed: debug is optional
Try it
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.
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
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:
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. WithexactOptionalPropertyTypesthe type must be writtenkey?: T(notT | undefined) fordeleteto be the "correct" way to remove it. - Arrays get holes.
delete arr[i]keepslengthand leaves an empty slot. Usesplice,filterortoSplicedto 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 anydelete. - Performance. Deleting properties can push V8 objects into slower dictionary mode. For hot objects, set the field to
undefinedor build a new object; for dynamic key sets use aMap. - Frozen, sealed and non-configurable properties cannot be deleted: strict code throws
TypeError, whileReflect.deletePropertyreturnsfalse. - Not for variables or functions.
deletenever removeslet,const,varor function bindings. - Optional chaining:
delete a?.bis valid and does nothing (returnstrue) whenais nullish.