简中文档

no-namespace

NOTE: this rule is part of the 推荐 rule set.
Enable full set in deno.json:
{
  "lint": {
    "rules": {
      "tags": ["推荐"]
    }
  }
}
Enable full set using the Deno CLI:
deno lint --rules-tags=推荐
This rule can be explictly included to or excluded from the rules present in the current tag by adding it to the include or exclude array in deno.json:
{
  "lint": {
    "rules": {
      "include": ["no-namespace"],
      "exclude": ["no-namespace"]
    }
  }
}

不允许在 TypeScript 代码中使用 namespacemodule 关键字。

namespacemodule 都被认为是过时的关键字,用于组织代码。相反,通常更倾向于使用 ES2015 模块语法(例如 import/export)。

然而,此规则仍然允许在以下两种情况下使用这些关键字:

  • 它们用于定义 "ambient" 命名空间 并与 declare 关键字一起使用
  • 它们写在 TypeScript 的类型定义文件中:.d.ts

无效:

// foo.ts
module mod {}
namespace ns {}
// bar.d.ts
// 在 `.d.ts` 中允许使用 `module` 和 `namespace` 关键字的所有情况

有效:

// 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 {}

你找到了你需要的东西吗?

编辑此页面
隐私政策