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) {
📋 Table of Contents
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
- Step 1: Create a type guard function that checks if x is an instance of Foo.
Method 2: Using the instanceof Operator with Classes
- Step 1: Create a class that extends Foo and use it to check if x is an instance of this class.
💡 Conclusion
[Wrap-up]
❓ Frequently Asked Questions
Step 1: Create a type guard function that checks if x is an instance of Foo.
Step 1: Create a class that extends Foo and use it to check if x is an instance of this class.
🛠️ Related Fixes
How to Fix: Stuck in tutorial hell after 4 years: How do I b
Learn to build websites and think independently with coding skills.
How to Fix: Trying to sync mutliple audio tracks to a movie
Complex audio track synchronization can be challenging due to the larg
How to Fix: Failed to merge latest branches from upstream re
Update local repository with latest upstream branches.