Coding⏱️ 2 min read📅 2026-05-31

How to Fix: Why does 'instanceof' in TypeScript give me the error "'Foo' only refers to a type, but is being used as a value here."?

Type checking issue with instanceof operator in TypeScript.

Quick Answer: The error occurs because instanceof checks if an object is of a specific type, not if it has the property. Use the in operator instead: `if (x in Foo) {

The error "Foo' only refers to a type, but is being used as a value here." occurs because the instanceof operator in TypeScript can't be directly applied to interfaces. This limitation arises from how TypeScript handles interface types and their usage.

💡 Why You Are Getting This Error

  • [Cause]

🔧 Proven Troubleshooting Steps

Method 1: Using the Type Guard Pattern

  1. Step 1: Create a type guard function that checks if x is an instance of Foo.

Method 2: Using the instanceof Operator with Classes

  1. Step 1: Create a class that extends Foo and use it to check if x is an instance of this class.

💡 Conclusion

[Wrap-up]

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions