Skip to content
TypeScript·Intermediate·Coding·1 min read

What is a type predicate?

Short interview answer

A return type such as value is User tells TypeScript that a true result narrows the argument. The implementation must actually validate that claim; the compiler trusts it, so an incorrect predicate creates unsoundness similar to a bad assertion.

Example

TypeScript
function isUser(v: unknown): v is User {  return typeof v === 'object' && v !== null && 'id' in v;}
if (isUser(payload)) {  payload.id; // narrowed to User}

Key takeaway

Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.

← Back to Type narrowing

Related questions