Skip to content

noUnsafeDeclarationMerging

诊断类别:lint/suspicious/noUnsafeDeclarationMerging

¥Diagnostic Category: lint/suspicious/noUnsafeDeclarationMerging

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

禁止接口和类之间不安全的声明合并。

¥Disallow unsafe declaration merging between interfaces and classes.

TypeScript 的 声明合并 支持合并具有相同名称的单独声明。

¥TypeScript’s declaration merging supports merging separate declarations with the same name.

类和接口之间的声明合并是不安全的。TypeScript 编译器不检查接口中定义的属性是否在类中初始化。这可能导致 TypeScript 无法检测到会导致运行时错误的代码。

¥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

¥Invalid

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.

¥Valid

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

¥Related links