typescript
/

TypeScript Namespaces

Last Sync: Today

On this page

10
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

typescript

TypeScript Namespaces

What are Namespaces?

Namespaces are a way to organize code into logical groups and prevent naming conflicts in TypeScript.

Basic Syntax

TypeScriptRead-only
1
namespace Utils {
  export function greet() {
    console.log('Hello');
  }
}

Using Namespace

TypeScriptRead-only
1
Utils.greet();

Nested Namespaces

TypeScriptRead-only
1
namespace App {
  export namespace Utils {
    export const version = '1.0';
  }
}

Alias Namespace

TypeScriptRead-only
1
import U = App.Utils;
console.log(U.version);

Namespace vs Module

FeatureNamespaceModule
UsageInternal organizationFile-based
Modern UsageLess usedPreferred
ScopeGlobalScoped per file

When to Use

  • Small projects
  • Legacy codebases
  • Avoid naming conflicts
  • Group related logic

Best Practices

  • Prefer modules over namespaces in modern apps
  • Use namespaces for internal grouping
  • Keep namespaces simple
  • Avoid deep nesting

Common Mistakes

  • Using namespaces instead of modules unnecessarily
  • Over-nesting namespaces
  • Not exporting members properly
  • Mixing namespaces with modules incorrectly

Conclusion

Namespaces help organize code and avoid naming conflicts, but modern TypeScript development prefers modules.

Try it yourself

namespace A { export const x = 10; }
A.x;

Test Your Knowledge

Q1
of 3

Namespace used for?

A
Styling
B
Organization
C
Routing
D
API
Q2
of 3

Modern alternative?

A
Classes
B
Modules
C
Enums
D
Functions
Q3
of 3

Namespace scope?

A
Local
B
Global
C
Private
D
Protected

Frequently Asked Questions

What is namespace?

A way to group code logically.

Are namespaces used today?

Mostly replaced by modules.

Why use namespace?

To avoid naming conflicts.

Previous

ts modules

Next

ts declaration files

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.