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.
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.
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.
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 moduleFunction expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript