@typescript-eslint/no-namespace
Disallow TypeScript namespace and module blocks
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:
[lint.rules]
"@typescript-eslint/no-namespace" = "off" # or "error"Every option, set to its default:
[lint.rules]
"@typescript-eslint/no-namespace" = ["warn", {
allowDeclarations = true,
allowDefinitionFiles = true,
}]Examples#
namespace Format {ES2015 module syntax is preferred over namespaces.
export function price(cents: number) {
return `$${cents / 100}`;
}
}
Format.price(1999);Not reported:
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#
| Option | Default | What it does |
|---|---|---|
allowDeclarations | true | Allow a namespace with declare, and the namespaces inside it. |
allowDefinitionFiles | true | Allow 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:
{ "allowDeclarations": false }1 problemdeclare namespace Charts {ES2015 module syntax is preferred over namespaces.
function draw(id: string): void;
}Not reported in a .d.ts file:
declare namespace Charts {
function draw(id: string): void;
}Reported there too if both options are false:
{ "allowDefinitionFiles": false, "allowDeclarations": false }1 problemdeclare namespace Charts {ES2015 module syntax is preferred over namespaces.
function draw(id: string): void;
}