@typescript-eslint/no-dynamic-delete
Disallow delete with a computed property key
Also called typescript-eslint/no-dynamic-delete, typescript/no-dynamic-delete
delete sessions[userId] with a key that's only known at run time means you're using the object as a map. TypeScript can't check that the key exists, and delete doesn't tell you whether it removed anything. Use a Map or a Set instead. Their .delete() returns true only if the key was there.
A literal key is fine: a string, a number, or a negative number.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"@typescript-eslint/no-dynamic-delete" = "error" # or "warn"Examples#
example.ts1 problem
const sessions: Record<string, Date> = {};
function logout(userId: string) {
delete sessions[userIdDo not delete dynamically computed property keys.];
}example.tsNo problems
const sessions = new Map<string, Date>();
function logout(userId: string) {
sessions.delete(userId);
}example.tsNo problems
const headers: Record<string, string> = {};
delete headers.cookie;
delete headers["set-cookie"];