Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a type alias named Greet for a function that takes a string and returns a string.
Typescript
type Greet = (name: string) => [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'number' or 'void' as the return type instead of 'string'.
Forgetting to specify the return type.
✗ Incorrect
The function type alias should specify the return type after the arrow. Since the function returns a string, the correct return type is 'string'.
2fill in blank
mediumComplete the code to declare a variable sayHello with the type alias Greet.
Typescript
let sayHello: [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' as the type instead of the function type alias.
Using 'Function' which is too general.
✗ Incorrect
To use the type alias for a function, assign the variable the type 'Greet'.
3fill in blank
hardFix the error in the function implementation to match the Greet type alias.
Typescript
sayHello = function(name: string): [1] { return "Hello, " + name; };
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' or 'number' as the return type causing type errors.
Omitting the return type.
✗ Incorrect
The function must return a string to match the 'Greet' type alias.
4fill in blank
hardFill both blanks to define a type alias MathOp for a function taking two numbers and returning a number.
Typescript
type MathOp = (a: [1], b: [2]) => number;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' or 'boolean' as parameter types.
Mixing different types for parameters.
✗ Incorrect
Both parameters should be of type 'number' to match the function signature.
5fill in blank
hardFill all three blanks to create a variable multiply of type MathOp that multiplies two numbers.
Typescript
const multiply: [1] = (x: [2], y: [3]) => x * y;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong types for parameters or variable type.
Forgetting to use the type alias for the variable.
✗ Incorrect
The variable 'multiply' must have type 'MathOp'. The parameters 'x' and 'y' must be numbers.