How to fix Cannot use namespace as a value in TypeScript
This error occurs when you try to use a TypeScript namespace as a runtime value. Since namespaces are compile-time constructs, they don't exist in the generated JavaScript and cannot be referenced like variables or objects.
Quick fix
Fixes Cannot use namespace as a value
namespace MyNamespace {
export const value = 42;
}
// This causes the error:
const obj = MyNamespace; // Cannot use namespace as a value
export default MyNamespace; // Cannot use namespace as a value
setTimeout(() => MyNamespace, 1000); // Cannot use namespace as a value- Identify where the namespace is being used as a value
- Replace the namespace with a const object or class if you need runtime values
- Use ES6 modules instead of namespaces for modern code
- Separate types from values if mixing namespaces and interfaces
namespace MyNamespace {export const value = 42;}// This causes the error:const obj = MyNamespace; // Cannot use namespace as a valueexport default MyNamespace; // Cannot use namespace as a valuesetTimeout(() => MyNamespace, 1000); // Cannot use namespace as a valueCannot use namespace as a value
What this error means
In TypeScript, namespaces are a code organization feature that only exist during compilation. They are purely a type-system construct and don't generate any runtime JavaScript objects. When you attempt to use a namespace where a value is expected—such as assigning it to a variable, passing it as a function argument, or exporting it as a default—TypeScript throws this error because namespaces have no runtime representation.
Typical symptoms
- Compiler error 'Cannot use namespace X as a value' appears when building
- Code that references a namespace as a variable or parameter fails to compile
- Default export attempts with namespaces produce this error
- Trying to instantiate or spread a namespace causes compilation failure
- Mixing namespace declarations with value exports creates conflicts
Common causes
- Attempting to use a namespace as a variable or object literal (most common)
- Trying to export a namespace as a default export when the module expects a value
- Referencing a namespace where a runtime object or class is needed
- Forgetting that namespaces only exist at compile-time and disappear after transpilation
- Using legacy namespace patterns in modern ES6 module code
How to fix "Cannot use namespace as a value"
Find the line causing the error. Look for code like:
namespace MyNamespace {
export const value = 42;
}
// This causes the error:
const obj = MyNamespace; // Cannot use namespace as a value
export default MyNamespace; // Cannot use namespace as a value
setTimeout(() => MyNamespace, 1000); // Cannot use namespace as a valueNamespaces can only be used as types, not as values.
If your namespace contains values that need to exist at runtime, convert it to a const object:
// Before (won't work as value):
namespace MyNamespace {
export const API_URL = 'https://api.example.com';
export function fetch() { /* ... */ }
}
// After (works as value):
const MyNamespace = {
API_URL: 'https://api.example.com',
fetch() { /* ... */ }
};
export default MyNamespace; // Now this works!The const object pattern works exactly like the namespace but exists at runtime and can be used as a value.
Convert namespace files to separate module files with imports/exports:
// Before (namespace):
namespace utils {
export function helper() { /* ... */ }
export const config = { /* ... */ };
}
// After (ES6 modules):
// utils.ts
export function helper() { /* ... */ }
export const config = { /* ... */ };
// other-file.ts
import * as utils from './utils'; // or
import { helper, config } from './utils';ES6 modules are the modern standard and don't have this limitation.
If you need both type definitions and runtime values, keep them separate:
// Before (mixes type and value):
namespace MyType {
export interface Data {
id: number;
}
export const instance = { id: 1 }; // Error: namespace can't be used as value
}
// After (separate concerns):
interface MyData {
id: number;
}
const MyInstance = { id: 1 };
export { MyData, MyInstance };Types (interfaces, type aliases) live in the type space; values live in the value space. Namespaces blur this distinction, causing confusion.
If you're working with type definition files (.d.ts), ensure your tsconfig.json is configured correctly:
{
"compilerOptions": {
"module": "commonjs", // or "esnext"
"target": "ES2020",
"declaration": true,
"declarationMap": true,
"skipLibCheck": true
}
}Namespaces in .d.ts files are a special case—they can represent global augmentation without generating runtime code. But never try to use them as values in regular .ts files.
Advanced notes
Namespaces are a legacy TypeScript feature, now considered deprecated in favor of ES6 modules. They were introduced when JavaScript didn't have a standardized module system. Modern TypeScript code should always prefer modules.
However, namespaces are still useful for type definitions in declaration files (.d.ts) to augment global scope or organize type definitions. The key rule: you can use namespaces as *types* (type annotations, interfaces), but never as *values* (variables, function arguments, exports).
Common mistake: trying to re-export a namespace:
namespace Utils { /* ... */ }
export { Utils }; // Error!
// Correct: export its contents
export * from './utils'; // or
export { Utils } from './namespace-file'; // if it's actually a moduleFrequently asked questions
- What does "Cannot use namespace as a value" mean?
- In TypeScript, namespaces are a code organization feature that only exist during compilation. They are purely a type-system construct and don't generate any runtime JavaScript objects. When you attempt to use a namespace where a value is expected—such as assigning it to a variable, passing it as a function argument, or exporting it as a default—TypeScript throws this error because namespaces have no runtime representation.
- How do I fix "Cannot use namespace as a value"?
- This error occurs when you try to use a TypeScript namespace as a runtime value. Since namespaces are compile-time constructs, they don't exist in the generated JavaScript and cannot be referenced like variables or objects.
- What causes "Cannot use namespace as a value"?
- Attempting to use a namespace as a variable or object literal (most common) Trying to export a namespace as a default export when the module expects a value Referencing a namespace where a runtime object or class is needed
- Is the "Cannot use namespace as a value" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚TypeScript Official Documentation
- 📖TypeScript Namespaces documentation(Official Docs)
- 📖TypeScript Namespaces and Modules comparison(Official Docs)