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.
Options
This rule, in its default state, does not require any argument. If you would like to enable one or more of the following you may pass an object with the options set as follows:
allowDeclarations
set totrue
will allow you todeclare
custom TypeScript modules and namespaces (Default:false
).allowDefinitionFiles
set totrue
will allow you todeclare
and use custom TypeScript modules and namespaces inside definition files (Default:true
).
Examples of code for the default { "allowDeclarations": false, "allowDefinitionFiles": true }
options:
- ❌ Incorrect
- ✅ Correct
module foo {}
namespace foo {}
declare module foo {}
declare namespace foo {}
declare module 'foo' {}
// anything inside a d.ts file
allowDeclarations
Examples of code for 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.