0
0
Typescriptprogramming~15 mins

What types exist only at compile time in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
What types exist only at compile time
📖 Scenario: Imagine you are learning TypeScript to write safer JavaScript code. You want to understand which types help you catch mistakes before running your program but do not exist when the program runs.
🎯 Goal: You will create some TypeScript types that exist only during compile time and see how they help check your code without affecting the final JavaScript output.
📋 What You'll Learn
Create a type alias called Fruit with exact string values 'apple', 'banana', and 'orange'.
Create an interface called Person with properties name (string) and age (number).
Create a variable favoriteFruit of type Fruit and assign it the value 'banana'.
Create a variable user of type Person with name 'Alice' and age 30.
Print the values of favoriteFruit and user.name.
💡 Why This Matters
🌍 Real World
TypeScript types help developers write safer code by catching mistakes early without slowing down the running program.
💼 Career
Understanding compile-time types is essential for frontend and backend developers using TypeScript to build reliable applications.
Progress0 / 4 steps
1
Create a type alias for fruits
Create a type alias called Fruit with the exact string values 'apple', 'banana', and 'orange'.
Typescript
Need a hint?

Use the type keyword and union of string literals.

2
Create an interface for a person
Create an interface called Person with properties name of type string and age of type number.
Typescript
Need a hint?

Use the interface keyword and define properties with types.

3
Create variables using the types
Create a variable called favoriteFruit of type Fruit and assign it the value 'banana'. Then create a variable called user of type Person with name set to 'Alice' and age set to 30.
Typescript
Need a hint?

Use const to declare variables with the types you created.

4
Print the variable values
Write two console.log statements to print the values of favoriteFruit and user.name.
Typescript
Need a hint?

Use console.log(favoriteFruit) and console.log(user.name).