Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a type alias named Age for the number type.
Typescript
type Age = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
string instead of number.Forgetting the semicolon at the end.
✗ Incorrect
The
Age type alias should represent the number type, so we write type Age = number;.2fill in blank
mediumComplete the code to create a type alias named Coordinates for an object with x and y properties of type number.
Typescript
type Coordinates = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string types for
x and y.Using an array instead of an object.
✗ Incorrect
The
Coordinates type alias should be an object with x and y as numbers, so we write { x: number; y: number }.3fill in blank
hardFix the error in the type alias declaration for Person which should have a name string and an optional age number.
Typescript
type Person = { name: string; age[1]: number }; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using colon instead of question mark for optional property.
Missing the question mark causes
age to be required.✗ Incorrect
To make
age optional, use a question mark after the property name: age?: number.4fill in blank
hardFill both blanks to create a type alias ID that can be either a string or a number.
Typescript
type ID = [1] | [2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
boolean or object instead of string or number.Forgetting the pipe symbol.
✗ Incorrect
The
ID type alias should allow either string or number, so use a union type: string | number.5fill in blank
hardFill all three blanks to create a type alias Response for an object with a status string, a data array of numbers, and an optional error string.
Typescript
type Response = { status: [1]; data: [2]; error[3]: string }; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
boolean instead of string for status.Forgetting the question mark for the optional
error property.✗ Incorrect
The
status is a string, data is an array of numbers (number[]), and error is optional, so it uses a question mark.