- Error
- Reported by the type checker
TS2790
The operand of a 'delete' operator must be optional.
delete is used on a required property under strictNullChecks. Make the property optional, or set it to a valid value instead.
What it means
Deleting a property leaves it undefined. If the property's type says it's always present, deleting it would break that promise, so only optional properties (or ones that accept undefined) may be deleted.
Example
interface User { name: string }
function clear(u: User) {
delete u.name;
}
export {};
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(3,10): error TS2790: The operand of a 'delete' operator must be optional.
Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.
How to fix
- Mark the property optional in the type:
name?: string. - Or assign a valid value instead of deleting it.