TS2790: The operand of a 'delete' operator must be optional
You're using `delete obj.prop` on a property that the object's type declares as required — TypeScript won't let you delete something that isn't allowed to be missing. Mark the property optional (`prop?: type`) if it's genuinely allowed to be absent, or set it to `undefined` instead of deleting it if the type must always have the key present.
What causes this error
The delete operator physically removes a key from an object — which contradicts a type declaring that property as required (present on every instance of that type). TypeScript added this check specifically because `delete`-ing a required property leaves the runtime object not actually matching its declared type anymore.
The Fix
interface User {
id: string;
nickname: string;
}
const user: User = { id: '1', nickname: 'Ada' };
// Wrong — nickname is required, TypeScript blocks the delete
delete user.nickname; // TS2790
// Fix 1 — mark the property optional if it's genuinely removable
interface User {
id: string;
nickname?: string;
}
delete user.nickname; // now allowed
// Fix 2 — if it must always be present, assign undefined instead of deleting
user.nickname = undefined as any; // only if the type truly allows it, otherwise reconsider the typeCommon causes / variations
- Cleaning up a temporary property on an object whose type doesn't mark it optional
- Removing a field before sending an object to an API, when the type should model that field as optional to begin with
- Porting older JavaScript code that freely deleted properties into a strictly-typed TypeScript object
Related errors
See also: TS2322 Type is not assignable, TS2532 Object is possibly undefined.
Comments
Loading comments...