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 delete operator is a unary operator that removes a specific own property from an object. Upon successful execution, the property is entirely detached from the object, and subsequent accesses to that property identifier will evaluate to undefined.

Syntax

delete object.property;
delete object["property"];

Return Value

The operator evaluates to a boolean.
  • Returns true if the property was successfully deleted, or if the property did not exist on the object in the first place.
  • Returns false in JavaScript’s runtime non-strict mode (or throws a TypeError in JavaScript’s runtime strict mode via "use strict") if the property is non-configurable, such as properties defined via Object.defineProperty() with configurable: false.

TypeScript Type Checking

TypeScript enforces strict structural typing rules around the delete operator to prevent runtime mutations that violate an object’s declared contract. When the strictNullChecks compiler option is enabled (which is included when enabling the broader TypeScript --strict flag), the compiler requires that the operand of a delete operation be an optional property. Attempting to delete a required property results in compiler error TS2790. Furthermore, attempting to delete a property marked as readonly triggers compiler error TS2794, regardless of whether the property is optional or required.
interface Entity {
  readonly id: string;  // Required, Read-only
  role: string;         // Required
  metadata?: string;    // Optional
}

const obj: Entity = {
  id: "123",
  role: "admin",
  metadata: "active"
};

delete obj.metadata; // Valid: Property is optional and mutable
delete obj.role;     // Error TS2790: The operand of a 'delete' operator must be optional.
delete obj.id;       // Error TS2794: The operand of a 'delete' operator cannot be a read-only property.
To bypass this for dynamic objects, the object must be typed with an index signature (e.g., Record<string, any> or {[key: string]: unknown}), which implicitly treats all properties as optional for deletion purposes.

Mechanics and Limitations

  • Prototype Chain: delete only operates on an object’s own properties. It does not traverse the prototype chain. If an own property is deleted but a property with the same key exists on the prototype, the prototype’s property will be exposed upon subsequent lookups.
  • Variables and Functions: The operator cannot delete variables declared with var, let, or const, nor can it delete standalone function declarations. It strictly targets object properties.
  • Arrays: When applied to an array index, delete removes the value at that specific index but does not mutate the array’s length property or shift subsequent elements. This creates a sparse array containing an empty slot.
const sequence: number[] = [10, 20, 30];
delete sequence[1];

console.log(sequence.length); // 3
console.log(sequence);        // [10, empty, 30]
console.log(sequence[1]);     // undefined
Master TypeScript with Deep Grasping Methodology!Learn More