0
0
Typescriptprogramming~30 mins

Runtime type checking strategies in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Runtime type checking strategies
📋 What You'll Learn
💡 Why This Matters
🌍 Real World
When working with data from APIs or user input, runtime type checking helps prevent errors by verifying data shapes before using them.
💼 Career
Many jobs require validating external data safely. Knowing runtime type checking strategies in TypeScript is useful for frontend and backend development.
Progress0 / 4 steps
1
Create the user data object
Create a constant object called userData with these exact properties and values: name as "Alice" (string), age as 30 (number), and isMember as true (boolean).
Typescript
Need a hint?

Use const userData = { name: "Alice", age: 30, isMember: true };

2
Define expected property names
Create a constant array called expectedProps containing the strings "name", "age", and "isMember".
Typescript
Need a hint?

Use const expectedProps = ["name", "age", "isMember"];

3
Write a runtime type check function
Write a function called isValidUser that takes a parameter obj of type any and returns true if obj has all properties in expectedProps with types string for name, number for age, and boolean for isMember. Otherwise, return false. Use typeof checks inside the function.
Typescript
Need a hint?

Check each property with typeof obj.property === "type" and combine with &&.

4
Print the runtime type check result
Write a console.log statement that prints the result of calling isValidUser(userData).
Typescript
Need a hint?

Use console.log(isValidUser(userData)); to show the check result.