0
0
Typescriptprogramming~10 mins

Type alias for functions in Typescript - Interactive Code Practice

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

Complete 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'
Aboolean
Bnumber
Cvoid
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'number' or 'void' as the return type instead of 'string'.
Forgetting to specify the return type.
2fill in blank
medium

Complete 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'
AFunction
BGreet
Cstring
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' as the type instead of the function type alias.
Using 'Function' which is too general.
3fill in blank
hard

Fix 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'
Astring
Bvoid
Cnumber
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' or 'number' as the return type causing type errors.
Omitting the return type.
4fill in blank
hard

Fill 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'
Anumber
Bstring
Cboolean
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' or 'boolean' as parameter types.
Mixing different types for parameters.
5fill in blank
hard

Fill 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'
AMathOp
Bnumber
Cstring
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong types for parameters or variable type.
Forgetting to use the type alias for the variable.