AphrodyBun GitHub

Runtime docs · Linter · Rules

@typescript-eslint/no-namespace

Disallow TypeScript namespace and module blocks

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

Reported:

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}`;
}
import * as Format from "./format";

Format.price(1999);
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:

declare namespace Charts {
  function draw(id: string): void;
}

Reported if allowDeclarations is false:

declare namespace Charts {
  function draw(id: string): void;
}

Not reported in a .d.ts file:

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

Reported there too if both options are false:

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