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 function using type alias vs inline type
What is the output of this TypeScript code when calling
printUserInfo(user)?Typescript
type User = { name: string; age: number };
const user: User = { name: "Alice", age: 30 };
function printUserInfo(u: User) {
console.log(`Name: ${u.name}, Age: ${u.age}`);
}
printUserInfo(user);Attempts:
2 left
💡 Hint
Check how the type alias
User is used to type the variable and function parameter.✗ Incorrect
The type alias
User defines the shape of the object. The variable user matches this shape, so the function prints the correct values.❓ Predict Output
intermediate2:00remaining
Output difference between inline type and type alias
What will this code output when calling
printUserInfo(user)?Typescript
const user = { name: "Bob", age: 25 }; function printUserInfo(u: { name: string; age: number }) { console.log(`Name: ${u.name}, Age: ${u.age}`); } printUserInfo(user);
Attempts:
2 left
💡 Hint
Inline types work the same as type aliases for typing function parameters.
✗ Incorrect
The inline type defines the expected shape directly in the function parameter. The object matches this shape, so the output is correct.
🔧 Debug
advanced2:00remaining
Why does this code cause a TypeScript error?
This code causes a TypeScript error. What is the reason?
Typescript
type Person = { name: string; age: number };
const person: Person = { name: "Eve" };
function greet(p: Person) {
console.log(`Hello, ${p.name}`);
}
greet(person);Attempts:
2 left
💡 Hint
Check if the object matches all properties defined in the type alias.
✗ Incorrect
The object assigned to 'person' lacks the required 'age' property, causing a TypeScript error.
🧠 Conceptual
advanced2:00remaining
Difference between type alias and inline type in function parameters
Which statement best describes the difference between using a type alias and an inline type for function parameters in TypeScript?
Attempts:
2 left
💡 Hint
Think about how type aliases help organize code.
✗ Incorrect
Type aliases let you name a type and reuse it, making code cleaner. Inline types are written directly where needed and are not reusable.
📝 Syntax
expert2:00remaining
Which option causes a syntax error?
Which of the following TypeScript snippets causes a syntax error?
Attempts:
2 left
💡 Hint
Check for missing semicolons or braces in type alias declarations.
✗ Incorrect
Option A is missing a semicolon in the type alias declaration, causing a syntax error.