🔷 TypeScript Intermediate

What is a type predicate in TypeScript?

Why Interviewers Ask This

Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.

Answer

A type predicate is the return type annotation paramName is Type used to create user-defined type guards — functions that tell TypeScript what type a value is after the function returns true. Syntax: function isString(value: unknown): value is string { return typeof value === "string"; }. Usage: if (isString(data)) { data.toUpperCase(); /* TypeScript knows data is string here */ }. Without the predicate (returning just boolean), TypeScript would not narrow the type inside the if-block. Type predicates can narrow to any type: function isUser(obj: unknown): obj is User { return typeof obj === "object" && obj !== null && "name" in obj; }. Assertion functions are related but use asserts value is Type — instead of wrapping in an if-block, assertion functions throw if the condition fails, and the type is narrowed after the call site. Use type predicates for complex runtime type checks that TypeScript's built-in narrowing cannot express.

Pro Tip

Back up your answer with a specific project or situation. Saying 'In my last TypeScript project, I used this when...' immediately makes your answer more credible and memorable.