0
0
Typescriptprogramming~30 mins

TypeScript Strict Mode and Why It Matters - Mini Project: Build & Apply

Choose your learning style9 modes available
TypeScript Strict Mode and Why It Matters
📖 Scenario: You are building a small TypeScript program to manage a list of users and their ages. You want to make sure your code is safe and free from common mistakes by using TypeScript's strict mode.
🎯 Goal: Create a TypeScript program that defines a list of users with their names and ages, uses strict typing to avoid errors, and prints the users who are adults (age 18 or older).
📋 What You'll Learn
Create an array of user objects with exact properties: name (string) and age (number)
Create a constant adultAge set to 18
Use a for loop with variables user to filter users who are adults
Print the names of users who are adults using console.log
💡 Why This Matters
🌍 Real World
Strict mode helps catch mistakes early in programs that manage user data, making apps safer and more reliable.
💼 Career
Many companies require strict TypeScript settings to ensure code quality and reduce bugs in production.
Progress0 / 4 steps
1
Create the users array with strict types
Create a constant array called users with these exact objects: { name: 'Alice', age: 17 }, { name: 'Bob', age: 22 }, and { name: 'Charlie', age: 15 }. Use explicit type annotation for the array as an array of objects with name as string and age as number.
Typescript
Need a hint?

Use const users: { name: string; age: number }[] = [...] to define the array with strict types.

2
Define the adult age threshold
Create a constant called adultAge and set it to the number 18.
Typescript
Need a hint?

Just create const adultAge = 18; below the users array.

3
Filter adult users using a for loop
Create an empty array called adults with the same type as users. Use a for loop with variable user to go through users. Inside the loop, if user.age is greater than or equal to adultAge, add user to the adults array using push.
Typescript
Need a hint?

Remember to declare adults as an empty array with the same type as users. Use for (const user of users) to loop.

4
Print the names of adult users
Use a for loop with variable adult to go through the adults array. Inside the loop, print the adult.name using console.log.
Typescript
Need a hint?

Use for (const adult of adults) and inside the loop console.log(adult.name).