0
0
Typescriptprogramming~20 mins

Why object types are needed in Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Object Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this TypeScript code?

Consider this TypeScript code that uses an object type to define a person's details. What will be printed to the console?

Typescript
type Person = { name: string; age: number };

const person: Person = { name: "Alice", age: 30 };
console.log(`Name: ${person.name}, Age: ${person.age}`);
AName: Alice, Age: 30
BName: undefined, Age: undefined
CName: , Age: 30
DName: Alice, Age: undefined
Attempts:
2 left
💡 Hint

Look at how the object person is created and how its properties are accessed.

🧠 Conceptual
intermediate
1:30remaining
Why do we use object types in TypeScript?

Which of the following best explains why object types are needed in TypeScript?

ATo make JavaScript run faster by converting objects to numbers.
BTo allow objects to be used as keys in arrays.
CTo define the shape and structure of objects so the compiler can check for correct property usage.
DTo prevent objects from being changed after creation.
Attempts:
2 left
💡 Hint

Think about what TypeScript adds on top of JavaScript regarding objects.

🔧 Debug
advanced
2:00remaining
What error does this TypeScript code produce?

Look at this code snippet. What error will TypeScript show?

Typescript
type Car = { make: string; year: number };

const myCar: Car = { make: "Toyota" };
console.log(myCar.year);
AProperty 'year' is missing in type '{ make: string; }' but required in type 'Car'.
BNo error, code runs fine.
CType 'Car' is not assignable to type 'string'.
DCannot assign type 'string' to type 'number'.
Attempts:
2 left
💡 Hint

Check if all required properties are present when creating myCar.

📝 Syntax
advanced
1:30remaining
Which option correctly defines an object type with optional properties?

In TypeScript, how do you define an object type where the property middleName is optional?

Atype Person = { firstName: string; middleName: string?; lastName: string };
Btype Person = { firstName: string; middleName: optional string; lastName: string };
Ctype Person = { firstName: string; middleName: string | undefined; lastName: string };
Dtype Person = { firstName: string; middleName?: string; lastName: string };
Attempts:
2 left
💡 Hint

Look for the correct syntax to mark a property as optional in TypeScript.

🚀 Application
expert
2:30remaining
How many properties does this object have at runtime?

Given this TypeScript code, how many properties does the user object have when the program runs?

Typescript
type User = { id: number; name: string; email?: string };

const user: User = { id: 1, name: "Bob" };

if (user.email) {
  console.log(user.email);
}
A3 properties
B2 properties
C1 property
D0 properties
Attempts:
2 left
💡 Hint

Optional properties may or may not exist on the object at runtime depending on assignment.