@typescript-eslint/no-namespace

Disallow TypeScript namespace and module blocks

warn by default

Also called typescript-eslint/no-namespace, typescript/no-namespace

A namespace is TypeScript's own way to group code. It isn't JavaScript syntax. It compiles to a function that fills in an object when the file runs. A file with export does the same job in plain JavaScript, and a project that uses both has two places to look for where a name comes from.

declare global {}, declare module "name" {} and declare namespace Name {} are allowed, with everything inside them, because they describe other code. It's the only way to add to NodeJS.ProcessEnv, JSX.IntrinsicElements or Express.Request. .d.ts files aren't checked by default.

This rule is on by default in TypeScript files.

Configure#

On by default as a warning. To turn it off:

bunfig.toml
[lint.rules]
"@typescript-eslint/no-namespace" = "off" # or "error"

Every option, set to its default:

bunfig.toml
[lint.rules]
"@typescript-eslint/no-namespace" = ["warn", {
  allowDeclarations = true,
  allowDefinitionFiles = true,
}]

Examples#

example.ts1 problem
namespace Format {
  export function price(cents: number) {
    return `$${cents / 100}`;
  }
}

Format.price(1999);

Not reported:

format.ts
export function price(cents: number) {
  return `$${cents / 100}`;
}
example.tsNo problems
import * as Format from "./format";

Format.price(1999);
example.tsNo problems
declare module "legacy-charts" {
  export function draw(id: string): void;
}

declare global {
  var analyticsId: string;
  namespace NodeJS {
    interface ProcessEnv {
      API_URL: string;
    }
  }
}

export {};

Options#

OptionDefaultWhat it does
allowDeclarationstrueAllow a namespace with declare, and the namespaces inside it.
allowDefinitionFilestrueAllow namespaces in .d.ts files.

With an ESLint config file allowDeclarations is false, as in typescript-eslint.

Not reported by default:

example.tsNo problems
declare namespace Charts {
  function draw(id: string): void;
}

Reported if allowDeclarations is false:

example.tswith { "allowDeclarations": false }1 problem
declare namespace Charts {
  function draw(id: string): void;
}

Not reported in a .d.ts file:

charts.d.tsNo problems
declare namespace Charts {
  function draw(id: string): void;
}

Reported there too if both options are false:

charts.d.tswith { "allowDefinitionFiles": false, "allowDeclarations": false }1 problem
declare namespace Charts {
  function draw(id: string): void;
}