no-namespace
Disallow TypeScript namespaces.
Extending "plugin:@typescript-eslint/recommended"
in an ESLint configuration enables this rule.
TypeScript historically allowed a form of code organization called "custom modules" (module Example {}
), later renamed to "namespaces" (namespace Example
).
Namespaces are an outdated way to organize TypeScript code.
ES2015 module syntax is now preferred (import
/export
).
This rule does not report on the use of TypeScript module declarations to describe external APIs (
declare module 'foo' {}
).
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-namespace": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-namespace": "error"
}
};
Try this rule in the playground ↗
Examples
Examples of code with the default options:
- ❌ Incorrect
- ✅ Correct
module foo {}
namespace foo {}
declare module foo {}
declare namespace foo {}
Open in Playgrounddeclare module 'foo' {}
// anything inside a d.ts file
Open in PlaygroundOptions
This rule accepts the following options:
type 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
Whether to allow declare
with custom TypeScript namespaces. Default: false
.
Examples of code with the { "allowDeclarations": true }
option:
- ❌ Incorrect
- ✅ Correct
module foo {}
namespace foo {}
Open in Playgrounddeclare module 'foo' {}
declare module foo {}
declare namespace foo {}
declare global {
namespace foo {}
}
declare module foo {
namespace foo {}
}
Open in PlaygroundExamples of code for the { "allowDeclarations": false }
option:
- ❌ Incorrect
- ✅ Correct
module foo {}
namespace foo {}
declare module foo {}
declare namespace foo {}
Open in Playgrounddeclare module 'foo' {}
Open in PlaygroundallowDefinitionFiles
Whether to allow declare
with custom TypeScript namespaces inside definition files. Default: true
.
Examples of code for the { "allowDefinitionFiles": true }
option:
- ❌ Incorrect
- ✅ Correct
// 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 {}
Open in Playgrounddeclare module 'foo' {}
// anything inside a d.ts file
Open in PlaygroundWhen Not To Use It
If your project uses TypeScript's CommonJS export syntax (export = ...
), you may need to use namespaces in order to export types from your module.
You can learn more about this at:
- TypeScript#52203, the pull request introducing
verbatimModuleSyntax
- TypeScript#60852, an issue requesting syntax to export types from a CommonJS module.
If your project uses this syntax, either because it was architected before modern modules and namespaces, or because a module option such as verbatimModuleSyntax
requires it, it may be difficult to migrate off of namespaces.
In that case you may not be able to use this rule for parts of your project.
You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.
Further Reading
- FAQ: I get errors from the
@typescript-eslint/no-namespace
and/orno-var
rules about declaring global variables - FAQ: How should I handle reports that conflict with verbatimModuleSyntax?
- TypeScript handbook entry on Modules
- TypeScript handbook entry on Namespaces
- TypeScript handbook entry on Namespaces and Modules