Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' for age instead of 'number'.
Using 'boolean' which is true/false, not suitable here.
✗ Incorrect
The age property should be a number because age is a numeric value.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' or 'object' instead of the custom type 'Person'.
Using 'any' which disables type checking.
✗ Incorrect
The variable user should be typed as Person to match the object structure.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' which causes an error accessing 'name'.
Using 'number' or 'any' which are not specific types.
✗ Incorrect
The function expects a Person type to access name property safely.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' as return type instead of 'number'.
Using 'age' as parameter name without type.
✗ Incorrect
The function takes a parameter named person of type Person and returns a number representing the age.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'User' instead of 'Person' as type name.
Mixing up property names or types.
✗ Incorrect
The type is named Person with properties name (string) and age (number).