no-namespace
注意: 此规则是
recommended
规则集的一部分。在
deno.json
中启用完整规则集{ "lint": { "tags": ["recommended"] } }
使用 Deno CLI 启用完整规则集
deno lint --tags=recommended
禁止在 TypeScript 代码中使用 namespace
和 module
关键字。
namespace
和 module
都被认为是过时的代码组织关键字。 相反,通常最好使用 ES2015 模块语法 (例如 import
/export
)。
但是,此规则仍然允许在以下两种情况下使用这些关键字
- 它们用于定义 “环境”命名空间 以及
declare
关键字 - 它们写在 TypeScript 的类型定义文件中:
.d.ts
无效
// foo.ts
module mod {}
namespace ns {}
// bar.d.ts
// all usage of `module` and `namespace` keywords are allowed in `.d.ts`
有效
// foo.ts
declare global {}
declare module mod1 {}
declare module "mod2" {}
declare namespace ns {}
// bar.d.ts
module mod1 {}
namespace ns1 {}
declare global {}
declare module mod2 {}
declare module "mod3" {}
declare namespace ns2 {}