no-unsafe-declaration-merging
Disallow unsafe declaration merging.
Extending "plugin:@typescript-eslint/recommended"
in an ESLint configuration enables this rule.
TypeScript's "declaration merging" supports merging separate declarations with the same name.
Declaration merging between classes and interfaces is unsafe. The TypeScript compiler doesn't check whether properties are initialized, which can cause lead to TypeScript not detecting code that will cause runtime errors.
interface Foo {
nums: number[];
}
class Foo {}
const foo = new Foo();
foo.nums.push(1); // Runtime Error: Cannot read properties of undefined.
module.exports = {
"rules": {
"@typescript-eslint/no-unsafe-declaration-merging": "error"
}
};
Try this rule in the playground ↗
Examples
- ❌ Incorrect
- ✅ Correct
interface Foo {}
class Foo {}
Open in Playgroundinterface Foo {}
class Bar implements Foo {}
namespace Baz {}
namespace Baz {}
enum Baz {}
namespace Qux {}
function Qux() {}
Open in PlaygroundOptions
This rule is not configurable.
When Not To Use It
If your project intentionally defines classes and interfaces with unsafe declaration merging patterns, this rule might not be for you. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.