prefer-namespace-keyword
要求使用
namespace关键字而不是module关键字来声明自定义 TypeScript 模块.
在 ESLint 配置 中扩展"plugin:@typescript-eslint/recommended" 可启用此规则。
此规则报告的一些问题可通过 --fix ESLint 命令行选项自动修复。
TypeScript 历史上允许一种称为 "自定义模块" (module Example {}) 的代码组织形式,后来重命名为 "namespaces" (namespace Example)。
¥TypeScript historically allowed a form of code organization called "custom modules" (module Example {}), later renamed to "namespaces" (namespace Example).
命名空间是一种过时的组织 TypeScript 代码的方式。现在首选 ES2015 模块语法(import/export)。
¥Namespaces are an outdated way to organize TypeScript code.
ES2015 module syntax is now preferred (import/export).
对于仍然使用自定义模块/命名空间的项目,最好将它们称为命名空间。此规则报告何时使用 module 关键字而不是 namespace。
¥For projects still using custom modules / namespaces, it's preferred to refer to them as namespaces.
This rule reports when the module keyword is used instead of namespace.
此规则不报告使用 TypeScript 模块声明来描述外部 API(
declare module 'foo' {})的情况。¥This rule does not report on the use of TypeScript module declarations to describe external APIs (
declare module 'foo' {}).
- 扁平配置
- 旧版配置
export default tseslint.config({
rules: {
"@typescript-eslint/prefer-namespace-keyword": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/prefer-namespace-keyword": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ 错误
- ✅ 正确
module Example {}
Open in Playgroundnamespace Example {}
declare module 'foo' {}
Open in Playground选项
该规则不可配置。
何时不使用它
¥When Not To Use It
如果你不使用 TypeScript 的旧 module/namespace 关键字,那么你将不需要此规则。
¥If you are not using TypeScript's older module/namespace keywords, then you will not need this rule.
进一步阅读
¥Further Reading