Skip to main content

no-namespace

Disallow TypeScript namespaces.

ESLint 配置 中扩展"plugin:@typescript-eslint/recommended" 可启用此规则。

TypeScript 历史上允许一种称为 "自定义模块" (module Example {}) 的代码组织形式,后来重命名为 "namespaces" (namespace Example)。命名空间是一种过时的组织 TypeScript 代码的方式。现在首选 ES2015 模块语法(import/export)。

¥TypeScript historically allowed a form of code organization called "custom modules" (module Example {}), later renamed to "namespaces" (namespace Example). Namespaces are an outdated way to organize TypeScript code. ES2015 module syntax is now preferred (import/export).

此规则不报告使用 TypeScript 模块声明来描述外部 API(declare module 'foo' {})的情况。

¥This rule does not report on the use of TypeScript module declarations to describe external APIs (declare module 'foo' {}).

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-namespace": "error"
}
});

在线运行试试这个规则 ↗

示例

¥Examples

带有默认选项的代码示例:

¥Examples of code with the default options:

module foo {}
namespace foo {}

declare module foo {}
declare namespace foo {}
Open in Playground

选项

该规则接受以下选项:

type Options = [
{
/** Whether to allow `declare` with custom TypeScript namespaces. */
allowDeclarations?: boolean;
/** Whether to allow `declare` with custom TypeScript namespaces inside definition files. */
allowDefinitionFiles?: boolean;
},
];

const defaultOptions: Options = [
{ allowDeclarations: false, allowDefinitionFiles: true },
];

¥Options

allowDeclarations

Whether to allow declare with custom TypeScript namespaces. Default: false.

带有 { "allowDeclarations": true } 选项的代码示例:

¥Examples of code with the { "allowDeclarations": true } option:

module foo {}
namespace foo {}
Open in Playground

{ "allowDeclarations": false } 选项的代码示例:

¥Examples of code for the { "allowDeclarations": false } option:

module foo {}
namespace foo {}
declare module foo {}
declare namespace foo {}
Open in Playground

allowDefinitionFiles

Whether to allow declare with custom TypeScript namespaces inside definition files. Default: true.

{ "allowDefinitionFiles": true } 选项的代码示例:

¥Examples of code for the { "allowDefinitionFiles": true } option:

// if outside a d.ts file
module foo {}
namespace foo {}

// if outside a d.ts file and allowDeclarations = false
module foo {}
namespace foo {}
declare module foo {}
declare namespace foo {}
Open in Playground

何时不使用它

¥When Not To Use It

如果你的项目使用 TypeScript 的 CommonJS 导出语法 (export = ...),则可能需要使用命名空间才能从模块中导出类型。你可以在以下位置了解更多信息:

¥If your project uses TypeScript's CommonJS export syntax (export = ...), you may need to use namespaces in order to export types from your module. You can learn more about this at:

如果你的项目使用此语法,无论是因为它是在现代模块和命名空间之前设计的,还是因为模块选项(如 verbatimModuleSyntax)需要它,都可能很难从命名空间迁移出来。在这种情况下,你可能无法在项目的某些部分使用此规则。

¥If your project uses this syntax, either because it was architected before modern modules and namespaces, or because a module option such as verbatimModuleSyntax requires it, it may be difficult to migrate off of namespaces. In that case you may not be able to use this rule for parts of your project.

你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。

¥You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.

进一步阅读

¥Further Reading

'## 资源'