📖 Scenario: You are building a simple contact list app. Each contact has a name and an email.To keep your code clean and easy to understand, you want to create a type alias for the contact object.
🎯 Goal: Create a type alias called Contact for an object with name and email properties, then create a variable using this type and print the contact's details.
📋 What You'll Learn
Create a type alias called Contact with name and email as string properties
Create a variable called myContact of type Contact with name 'Alice' and email 'alice@example.com'
Print the contact's name and email using console.log
💡 Why This Matters
🌍 Real World
Type aliases help keep code organized and clear when working with objects like user profiles, settings, or data records.
💼 Career
Understanding type aliases is important for writing clean, maintainable TypeScript code in many software development jobs.
Progress0 / 4 steps
1
Create the Contact type alias
Create a type alias called Contact for an object with two string properties: name and email.
Typescript
Hint
Use the type keyword followed by the alias name and an object shape with properties.
2
Create a variable of type Contact
Create a variable called myContact of type Contact and assign it an object with name set to 'Alice' and email set to 'alice@example.com'.
Hint: Access object properties by name to get values [OK]
Common Mistakes:
Expecting property names printed instead of values
Confusing syntax causing errors
Forgetting to match object shape to type alias
4. Identify the error in this TypeScript code using a type alias:
type User = { username: string; age: number };
const user: User = { username: "Alice" };
medium
A. Missing property 'age' in the object assigned to 'user'
B. Incorrect type alias syntax
C. Variable 'user' should be declared with 'var' instead of 'const'
D. Extra property 'username' not allowed
Solution
Step 1: Check the type alias requirements
The type alias User requires both username and age properties.
Step 2: Compare with the object assigned
The object only has username, missing age, causing a type error.
Final Answer:
Missing property 'age' in the object assigned to 'user' -> Option A
Quick Check:
All required properties must be present [OK]
Hint: All properties in type alias must appear in assigned object [OK]
Common Mistakes:
Ignoring missing required properties
Thinking 'const' causes error here
Confusing extra vs missing properties
5. You want to create a type alias Product with properties id (number), name (string), and an optional description (string). Which is the correct way to define it?
hard
A. type Product = { id: number; name: string; description: string?; };
B. type Product = { id: number; name: string; description: string | undefined; };
C. type Product = { id: number; name: string; description?: string; };
D. type Product = { id: number; name: string; description: optional string; };
Solution
Step 1: Recall how to mark optional properties in type aliases
Optional properties use a question mark after the property name, like description?: string.
Step 2: Check each option for correct optional syntax
type Product = { id: number; name: string; description?: string; }; uses the correct syntax. type Product = { id: number; name: string; description: string | undefined; }; uses a union with undefined but is less idiomatic. Options A and C use invalid syntax.
Final Answer:
type Product = { id: number; name: string; description?: string; }; -> Option C
Quick Check:
Optional property = question mark after name [OK]
Hint: Use '?' after property name to mark optional [OK]
Common Mistakes:
Using 'optional' keyword instead of '?'
Placing '?' after type instead of property name
Using union with undefined unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.