typescript/no-namespace 限制
它做了什么
禁止使用 TypeScript 命名空间。
为什么这是不好的?
在历史上,TypeScript 允许一种称为“自定义模块”(module Example {})的代码组织形式,后来被重命名为“命名空间”(namespace Example)。命名空间是一种过时的 TypeScript 代码组织方式。如今推荐使用 ES2015 模块语法(import/export)。
示例
此规则的错误代码示例:
typescript
module foo {}
namespace foo {}
declare module foo {}
declare namespace foo {}此规则的正确代码示例:
typescript
declare module "foo" {}
// 任何位于 d.ts 文件中的内容配置
此规则接受一个包含以下属性的配置对象:
allowDeclarations
type: boolean
default: false
是否允许使用自定义 TypeScript 命名空间的 declare。
当 { "allowDeclarations": true } 时,此规则的错误代码示例:
typescript
module foo {}
namespace foo {}当 { "allowDeclarations": true } 时,此规则的正确代码示例:
typescript
declare module "foo" {}
declare module foo {}
declare namespace foo {}
declare global {
namespace foo {}
}
declare module foo {
namespace foo {}
}当 { "allowDeclarations": false } 时,此规则的错误代码示例:
typescript
module foo {}
namespace foo {}
declare module foo {}
declare namespace foo {}当 { "allowDeclarations": false } 时,此规则的正确代码示例:
typescript
declare module "foo" {}allowDefinitionFiles
type: boolean
default: true
当 { "allowDefinitionFiles": true } 时,此规则的错误代码示例:
typescript
// 如果不在 d.ts 文件中
module foo {}
namespace foo {}
// 如果不在 d.ts 文件中
module foo {}
namespace foo {}
declare module foo {}
declare namespace foo {}当 { "allowDefinitionFiles": true } 时,此规则的正确代码示例:
typescript
declare module "foo" {}
// 任何位于 d.ts 文件中的内容如何使用
要通过配置文件或 CLI 启用此规则,可以使用:
json
{
"rules": {
"typescript/no-namespace": "error"
}
}bash
oxlint --deny typescript/no-namespace