Challenge - 5 Problems
Type Alias Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of accessing properties in a type alias object
What is the output of this TypeScript code when compiled and run with Node.js?
Typescript
type Person = { name: string; age: number };
const user: Person = { name: "Alice", age: 30 };
console.log(user.name + " is " + user.age + " years old.");Attempts:
2 left
💡 Hint
Look at how the object is created and how properties are accessed.
✗ Incorrect
The type alias Person defines an object with name and age. The object user matches this type and has valid values. Accessing user.name and user.age prints the expected string.
🧠 Conceptual
intermediate2:00remaining
Understanding type alias with optional properties
Given this type alias, which statement is true about the object assignment below?
Typescript
type Car = { make: string; model: string; year?: number };
const myCar: Car = { make: "Toyota", model: "Corolla" };Attempts:
2 left
💡 Hint
Check the question mark in the type alias definition.
✗ Incorrect
The question mark after year means it is optional. So the object can omit year and still be valid.
🔧 Debug
advanced2:00remaining
Identify the error in this type alias usage
What error will this TypeScript code produce?
Typescript
type Point = { x: number; y: number };
const p: Point = { x: 10, y: "20" };Attempts:
2 left
💡 Hint
Look at the type of y in the object and in the type alias.
✗ Incorrect
The type alias expects y to be a number, but the object assigns a string "20". This causes a type error.
📝 Syntax
advanced2:00remaining
Correct syntax for type alias with nested objects
Which option correctly defines a type alias for an object with a nested address object?
Attempts:
2 left
💡 Hint
Nested objects use curly braces, not arrays or parentheses.
✗ Incorrect
Option A correctly uses an object type for address with street and city as strings.
🚀 Application
expert2:00remaining
Determine the number of keys in a type alias object at runtime
Given this type alias and object, what is the output of the code below?
Typescript
type Config = { host: string; port: number; secure?: boolean };
const serverConfig: Config = { host: "localhost", port: 8080 };
console.log(Object.keys(serverConfig).length);Attempts:
2 left
💡 Hint
Optional properties not set do not appear in the object keys.
✗ Incorrect
Only host and port are present in the object, so Object.keys returns 2 keys.