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' {}
).
module.exports = {
"rules": {
"@typescript-eslint/no-namespace": "error"
}
};
Examples
Examples of code with the default options:
- ❌ Incorrect
- ✅ Correct
module foo {}
namespace foo {}
declare module foo {}
declare namespace foo {}
declare module 'foo' {}
// anything inside a d.ts file
Options
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
Examples of code with the { "allowDeclarations": true }
option:
- ❌ Incorrect
- ✅ Correct
module foo {}
namespace foo {}
declare module 'foo' {}
declare module foo {}
declare namespace foo {}
declare global {
namespace foo {}
}
declare module foo {
namespace foo {}
}
Examples of code for the { "allowDeclarations": false }
option:
- ❌ Incorrect
- ✅ Correct
module foo {}
namespace foo {}
declare module foo {}
declare namespace foo {}
declare module 'foo' {}
allowDefinitionFiles
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 {}
declare module 'foo' {}
// anything inside a d.ts file
When Not To Use It
If you are using the ES2015 module syntax, then you will not need this rule.