# @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.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

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

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

Every option, set to its default:

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

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```ts
namespace Format {
  export function price(cents: number) {
    return `$${cents / 100}`;
  }
}

Format.price(1999);
```

Not reported:

{/* file "format.ts" */}
```ts format.ts
export function price(cents: number) {
  return `$${cents / 100}`;
}
```

{/* good */}
```ts
import * as Format from "./format";

Format.price(1999);
```

{/* good */}
```ts
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:

{/* good */}
```ts
declare namespace Charts {
  function draw(id: string): void;
}
```

Reported if `allowDeclarations` is `false`:

{/* bad {"options": [{"allowDeclarations": false}]} */}
```ts
declare namespace Charts {
  function draw(id: string): void;
}
```

Not reported in a `.d.ts` file:

{/* good {"filename": "charts.d.ts"} */}
```ts charts.d.ts
declare namespace Charts {
  function draw(id: string): void;
}
```

Reported there too if both options are `false`:

{/* bad {"filename": "charts.d.ts", "options": [{"allowDefinitionFiles": false, "allowDeclarations": false}]} */}
```ts charts.d.ts
declare namespace Charts {
  function draw(id: string): void;
}
```
