Q09. What is a type predicate?
A type predicate is a function return annotation such as value is User that tells TypeScript a true result narrows the value to a type. The implementation must genuinely check the required conditions because the compiler trusts the predicate declaration.
The predicate does not convert an object. It communicates a narrowing relationship to the compiler, so incorrect logic can create unsoundness.
Note
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
Type predicate
function isString(value: unknown): value is string {
return typeof value === "string";
}💡 The predicate tells TypeScript how a true result narrows the input.
Key Takeaways
- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
Knowledge Check
What does `value is User` mean in a return annotation?