What is a type predicate? Write a custom type guard for User.
Tests if you know a type predicate is a compile-time narrowing hint requiring runtime validation. Great answers define parameter is Type, validate every User property, and demonstrate narrowing. Red flag: omitting runtime checks or using as casting.
WHAT THIS TESTS: This question probes your understanding of TypeScript control-flow based narrowing, specifically user-defined type guards. The interviewer wants to see that you know a type predicate is a compile-time annotation that tells the type checker to refine a union or unknown type to a more specific type within a conditional branch. They also want to see that you understand the distinction between compile-time types and runtime values, because the predicate only affects the type system and does not automatically validate anything at runtime.
A GOOD ANSWER COVERS: First, define a type predicate as a function whose return type is written as parameter is Type, which signals to TypeScript that the parameter should be narrowed when the function returns true. Second, write a runtime validation body that actually checks the shape of the object, such as verifying the value is a non-null object and that every required property exists with the correct primitive type. Third, demonstrate usage inside an if block where the guarded variable is automatically narrowed to the User type, allowing property access without additional casting. Fourth, mention that the predicate and the runtime check must agree; if they do not, the type system becomes unsound.
COMMON WRONG ANSWERS: A red flag is writing a predicate that returns true without inspecting the value, because this lies to the compiler and causes runtime errors. Another mistake is confusing a type guard with a type assertion like as User; assertions force the compiler to trust you, while guards provide conditional narrowing. Some candidates also forget to handle the null case or fail to check that properties have the correct types, producing guards that are too permissive.
LIKELY FOLLOW-UPS: The interviewer may ask what happens if the predicate is unsound, such as returning true for an object that lacks required fields. They might also ask how to narrow discriminated unions without a custom guard, or when to prefer instanceof over a user-defined type guard. A senior candidate should also be ready to discuss exhaustiveness checking and how to combine multiple guards.
ONE CONCRETE EXAMPLE: Define interface User with id as number, name as string, and email as string. Then write function isUser that accepts value of type unknown and returns value is User. The body first checks that value is a non-null object, then verifies that id is a number, name is a string, and email is a string before returning true. Inside a conditional such as if isUser maybeUser then maybeUser dot id dot toFixed, the compiler treats maybeUser as User without any manual casting.
Read the original → typescriptlang.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.