midTypeScript
Explain 'Type Predicates' and their use in custom Type Guards.
Updated May 4, 2026
Short answer
A type predicate is a special return type for a function (parameterName is Type) that tells the compiler to narrow a type if the function returns true.
Deep explanation
Standard boolean-returning functions do not narrow types in the calling scope. Custom type guards solve this by using the is keyword. When the function returns true, the TypeScript compiler is 'informed' that the variable passed as an argument is of the specified type, enabling safe access to that type's members within the conditional block.
Real-world example
Filtering an array containing null or undefined values to get a cleanly typed array of objects.
Common mistakes
- Writing logic inside the guard that doesn't actually match the type being asserted, which leads to runtime errors despite a green compiler.
Follow-up questions
- Can type predicates be used with classes?