跳转到内容

noUnsafeDeclarationMerging

此内容尚不支持你的语言。

Diagnostic Category: lint/suspicious/noUnsafeDeclarationMerging

Since: v1.0.0

Sources:

Disallow 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.

interface 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.

interface Foo {}
class Bar implements Foo {}
namespace Baz {}
namespace Baz {}
enum Baz {}