TS2511
TypeScriptERRORNotableNamespacesHIGH confidence

Cannot find namespace

What this means

TypeScript cannot resolve a reference to a namespace.

Why it happens
  1. 1A typo in the namespace name.
  2. 2The file containing the namespace has not been included in the compilation.
  3. 3Attempting to use a namespace from an external module without proper imports or type declarations.
How to reproduce

Referencing a namespace that has not been defined or imported.

trigger — this will error
trigger — this will error
MyNamespace.doSomething();

expected output

error TS2503: Cannot find namespace 'MyNamespace'.

Fix 1

Define the namespace

WHEN The namespace does not exist yet.

Define the namespace
namespace MyNamespace {
  export function doSomething() {}
}
MyNamespace.doSomething();

Why this works

Defining the namespace makes it available for use.

Fix 2

Include the namespace file in compilation

WHEN The namespace is in another file.

Include the namespace file in compilation
// In tsconfig.json
// "files": ["./path/to/namespace.ts", "./path/to/main.ts"]
// Or use a triple-slash directive in main.ts
/// <reference path="./path/to/namespace.ts" />
MyNamespace.doSomething();

Why this works

Making the compiler aware of the file containing the namespace allows it to be resolved.

What not to do

Declare a dummy namespace to satisfy the compiler

This provides no implementation and will lead to a runtime error when the code is executed.

Sources
Official documentation ↗

microsoft/TypeScript src/compiler/diagnosticMessages.json

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All TypeScript errors