@typescript-eslint/no-unsafe-declaration-merging
Disallow a class and an interface with the same name
warn by default
Also called typescript-eslint/no-unsafe-declaration-merging, typescript/no-unsafe-declaration-merging
A class and an interface with the same name in the same scope merge into one type. The compiler then believes that every instance has the interface's properties, but it never checks that the class sets them. Reading one gives you undefined where the type promises a value.
Both declarations are reported.
This rule is on by default in TypeScript files.
Configure#
On by default as a warning. To turn it off:
bunfig.toml
[lint.rules]
"@typescript-eslint/no-unsafe-declaration-merging" = "off" # or "error"Examples#
example.ts2 problems
interface UserUnsafe declaration merging between classes and interfaces. {
name: string;
}
class UserUnsafe declaration merging between classes and interfaces. {}
// No compile error, but it throws when it runs.
console.log(new User().name.length);example.tsNo problems
interface Named {
name: string;
}
class User implements Named {
name = "anonymous";
}
console.log(new User().name.length);Other kinds of merging aren't reported:
example.tsNo problems
interface Options {
verbose: boolean;
}
interface Options {
color: boolean;
}
class Logger {}
namespace Logger {
export const level = "info";
}