This error occurs when you try to use a type, interface, or type alias at runtime where a value is expected. In TypeScript, types only exist during compilation and are erased at runtime, so they cannot be used as JavaScript values.
TypeScript has two separate namespaces: types and values. Type definitions (interfaces, type aliases, abstract classes) exist only during compilation and are completely removed when your TypeScript code is transpiled to JavaScript. When you try to use a type name in a context that requires a runtime value—such as with the `instanceof` operator, function calls, or object properties—TypeScript throws this error because that type name doesn't exist as a value at runtime. For example, you can use `Employee` as a type annotation in `let emp: Employee`, but you cannot use it as a value in `instanceof Employee` because there's no actual JavaScript value to check against.
First, locate the line causing the error. You'll see something like:
interface User {
id: number;
name: string;
}
// ❌ Error: Cannot use 'User' as a value
if (obj instanceof User) {
console.log("It's a User");
}The error occurs because User is an interface (compile-time only), not a runtime value.
The simplest fix is to change the interface to a class. Classes exist at runtime and can be used with instanceof:
// ✅ Instead of interface, use class
class User {
constructor(
readonly id: number,
readonly name: string
) {}
}
if (obj instanceof User) {
console.log("It's a User");
}Classes provide both type-checking (like interfaces) and runtime values.
If you need to keep the interface, create a runtime function to check the properties:
interface User {
id: number;
name: string;
}
// Define a type guard function
function isUser(obj: unknown): obj is User {
return (
typeof obj === "object" &&
obj !== null &&
"id" in obj &&
"name" in obj &&
typeof (obj as any).id === "number" &&
typeof (obj as any).name === "string"
);
}
// ✅ Use the type guard instead of instanceof
if (isUser(obj)) {
console.log("It's a User");
}If you're importing a type that doesn't exist as a value, ensure you're using import type:
// ✅ Correct - type-only import
import type { User } from "./types";
// Use User only in type annotations, never as a value
let user: User;
// ❌ This would still error
// const UserClass = User; // Can't use User as a valueType-only imports explicitly tell TypeScript to erase the import, preventing accidental runtime usage.
Compile your TypeScript to ensure the error is gone:
npx tsc --noEmitOr run your dev server:
npm run devThe error should now be resolved, and your code will compile successfully.
### Type Assertion Workaround (Not Recommended)
In some cases, you might see a workaround using type assertions with typeof:
type UserType = typeof SomeClass;This only works when there's an underlying value (like a class). For pure interfaces, this approach fails.
### Discriminated Unions as a Type-Safe Alternative
Instead of relying on instanceof, use discriminated unions for better type safety:
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; side: number };
function getArea(shape: Shape) {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.side ** 2;
}
}This pattern is more idiomatic in TypeScript and avoids runtime type checking entirely.
### Zod and Runtime Validation Libraries
For complex data validation, consider libraries like Zod, io-ts, or Yup:
import { z } from "zod";
const UserSchema = z.object({
id: z.number(),
name: z.string(),
});
type User = z.infer<typeof UserSchema>;
// ✅ Safe runtime validation
const user = UserSchema.parse(unknownData);These libraries provide both type safety and runtime validation.
### Enum as a Type-Safe Alternative
For a fixed set of values, use enums instead of type unions:
enum UserRole {
Admin = "admin",
User = "user",
Guest = "guest",
}
// Enums exist at runtime, so this works
function checkRole(role: unknown): role is UserRole {
return Object.values(UserRole).includes(role);
}Function 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