namespaceDeclarations
Reports using legacy
namespace
declarations.
✅ This rule is included in the ts logical
preset.
Namespaces are a legacy feature of TypeScript that can lead to confusion and are not compatible with ECMAScript modules.
Modern codebases generally use export
and import
statements to define and use ECMAScript modules instead.
Examples
Section titled “Examples”namespace Values { export const value = 123;}
namespace Fruits { export const apple = "🍎";}
export const value = 123;
export const Fruits = { apple: "🍎",};
Options
Section titled “Options”allowDeclarations
Section titled “allowDeclarations”Whether to allow namespaces declared with the declare
keyword.
ts.rules({ namespaceDeclarations: { allowDeclarations: true, },});
namespace Values { export const value = 123;}
declare namespace Values { const value: number;}
allowDefinitionFiles
Section titled “allowDefinitionFiles”ts.rules({ namespaceDeclarations: { allowDefinitionFiles: true, },});
Whether to allow namespaces in .d.ts
and other definition files.
declare namespace Values { const value: number;}
declare namespace Values { const value: number;}
When Not To Use It
Section titled “When 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 Flint disable comments and/or configuration file disables for those specific situations instead of completely disabling this rule.
Further Reading
Section titled “Further Reading”- TypeScript handbook entry on Modules
- TypeScript handbook entry on Namespaces
- TypeScript handbook entry on Namespaces and Modules
Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
noNamespace
- Deno:
no-namespace
- ESLint:
@typescript-eslint/no-namespace
- Oxlint:
typescript/no-namespace