noUnsafeDeclarationMerging
Diagnostic Category: lint/suspicious/noUnsafeDeclarationMerging
Since: v1.0.0
Sources:
Description
Section titled DescriptionDisallow unsafe declaration merging between interfaces and classes.
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 defined in the interface are initialized in the class. This can cause lead to TypeScript not detecting code that will cause runtime errors.
Examples
Section titled ExamplesInvalid
Section titled Invalidinterface Foo { f(): void}
class Foo {}
const foo = new Foo();foo.f(); // Runtime Error: Cannot read properties of undefined.
code-block.ts:5:7 lint/suspicious/noUnsafeDeclarationMerging ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This class is unsafely merged with an interface.
3 │ }
4 │
> 5 │ class Foo {}
│ ^^^
6 │
7 │ const foo = new Foo();
ℹ The interface is declared here.
> 1 │ interface Foo {
│ ^^^
2 │ f(): void
3 │ }
ℹ The TypeScript compiler doesn’t check whether properties defined in the interface are initialized in the class.
Valid
Section titled Validinterface Foo {}class Bar implements Foo {}
namespace Baz {}namespace Baz {}enum Baz {}
How to configure
Section titled How to configure{ "linter": { "rules": { "suspicious": { "noUnsafeDeclarationMerging": "error" } } }}