0
0
Typescriptprogramming~10 mins

Why object types are needed in Typescript - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an object type for a person with a name and age.

Typescript
type Person = { name: string; age: [1] };
Drag options to blanks, or click blank then click option'
Aboolean
Bnumber
Cstring
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' for age instead of 'number'.
Using 'boolean' which is true/false, not suitable here.
2fill in blank
medium

Complete the code to create a variable with the Person type.

Typescript
const user: [1] = { name: 'Alice', age: 30 };
Drag options to blanks, or click blank then click option'
APerson
Bobject
Cstring
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' or 'object' instead of the custom type 'Person'.
Using 'any' which disables type checking.
3fill in blank
hard

Fix the error in the function parameter type to accept a Person object.

Typescript
function greet(person: [1]) { console.log(`Hello, ${person.name}`); }
Drag options to blanks, or click blank then click option'
Astring
Bany
Cnumber
DPerson
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' which causes an error accessing 'name'.
Using 'number' or 'any' which are not specific types.
4fill in blank
hard

Fill both blanks to create a function that returns the person's age.

Typescript
function getAge([1]: Person): [2] { return [1].age; }
Drag options to blanks, or click blank then click option'
Aperson
Bnumber
Cstring
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' as return type instead of 'number'.
Using 'age' as parameter name without type.
5fill in blank
hard

Fill all three blanks to create a typed object and access its properties.

Typescript
type [1] = { [2]: string; [3]: number };
Drag options to blanks, or click blank then click option'
APerson
Bname
Cage
DUser
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'User' instead of 'Person' as type name.
Mixing up property names or types.