← All Exercises

Custom Type Guard Functions

intermediatetypescript~20min

Background

Type guards enable runtime type checking with compile-time type narrowing, essential for parsing external data.

Problem

Create a type guard function isUser that checks if an unknown value is a User object with required properties, and use it for safe data access.

Input

A value of type unknown to check.

Output

A boolean indicating whether the value is a User, with type narrowing if true.

Sample tests

Sample 1

Input: {id:1, name:"Alice"}
Expected: true (narrowed to User)

Your solution

Learning objective

Learn custom type guard functions with the is operator for runtime type checking.