Skip to main content

no-namespace

Disallow custom TypeScript modules and namespaces.

Custom TypeScript modules (module foo {}) and namespaces (namespace foo {}) are considered outdated ways to organize TypeScript code. ES2015 module syntax is now preferred (import/export).

This rule still allows the use of TypeScript module declarations to describe external APIs (declare module 'foo' {}).

Attributes

  • Included in configs
    • ✅ Recommended
    • 🔒 Strict
  • Fixable
    • 🔧 Automated Fixer
    • 💡 Suggestion Fixer
  • 💭 Requires type information

Rule Details

This rule aims to standardize the way modules are declared.

Examples of code with the default options:

module foo {}
namespace foo {}

declare module foo {}
declare namespace foo {}
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-namespace": "error"
}
};

Options

This rule accepts an options object with the following properties:

interface Options {
/**
* Whether to allow `declare` with custom TypeScript namespaces.
*/
allowDeclarations?: boolean;
/**
* Whether to allow `declare` with custom TypeScript namespaces inside definition files.
*/
allowDefinitionFiles?: boolean;
}

const defaultOptions: Options = [
{ allowDeclarations: false, allowDefinitionFiles: true },
];

allowDeclarations

Examples of code with the { "allowDeclarations": true } option:

module foo {}
namespace foo {}

Examples of code for the { "allowDeclarations": false } option:

module foo {}
namespace foo {}
declare module foo {}
declare namespace foo {}

allowDefinitionFiles

Examples of code for the { "allowDefinitionFiles": true } option:

// if outside a d.ts file
module foo {}
namespace foo {}

// if outside a d.ts file and allowDeclarations = false
module foo {}
namespace foo {}
declare module foo {}
declare namespace foo {}

When Not To Use It

If you are using the ES2015 module syntax, then you will not need this rule.

Further Reading

Resources