0
0
Typescriptprogramming~20 mins

Type alias for objects in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Type Alias Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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.");
ACompilation error
Bundefined is undefined years old.
CTypeError at runtime
DAlice is 30 years old.
Attempts:
2 left
💡 Hint
Look at how the object is created and how properties are accessed.
🧠 Conceptual
intermediate
2: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" };
AThe object is valid because year is optional.
BThe object is invalid because year is missing.
CThe object is invalid because model is missing.
DThe object is invalid because make is optional but missing.
Attempts:
2 left
💡 Hint
Check the question mark in the type alias definition.
🔧 Debug
advanced
2: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" };
AProperty 'y' is missing in object.
BType 'string' is not assignable to type 'number'.
CNo error, code runs fine.
DType 'number' is not assignable to type 'string'.
Attempts:
2 left
💡 Hint
Look at the type of y in the object and in the type alias.
📝 Syntax
advanced
2:00remaining
Correct syntax for type alias with nested objects
Which option correctly defines a type alias for an object with a nested address object?
Atype User = { name: string; address: { street: string; city: string } };
Btype User = { name: string; address: [street: string, city: string] };
Ctype User = { name: string; address: (street: string, city: string) };
Dtype User = { name: string; address: string, city: string };
Attempts:
2 left
💡 Hint
Nested objects use curly braces, not arrays or parentheses.
🚀 Application
expert
2: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);
A3
B1
C2
D0
Attempts:
2 left
💡 Hint
Optional properties not set do not appear in the object keys.