Menu
Coddy logo textTech

Defining Function Types

Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 28 of 73.

TypeScript allows you to create a type alias for a function's signature, which acts like a blueprint that functions must match.

A function type alias defines the shape of a function using the type keyword. The syntax shows the parameter types in parentheses followed by an arrow and the return type:

type MathOperation = (a: number, b: number) => number;

const add: MathOperation = (a, b) => {
  return a + b;
};

const multiply: MathOperation = (a, b) => {
  return a * b;
};

In this example, MathOperation is a type alias that describes any function taking two numbers and returning a number. Both add and multiply conform to this type, ensuring consistency across your codebase.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create a type alias named StringProcessor for a function that takes a single string parameter and returns a string.

Create a type alias named NumberCalculator for a function that takes two number parameters and returns a number.

Create a type alias named BooleanChecker for a function that takes a string parameter and returns a boolean.

Now implement the following functions that conform to these type aliases:

  • Create a function named toUpperCase of type StringProcessor that converts the input string to uppercase
  • Create a function named addPrefix of type StringProcessor that adds the prefix "Processed: " to the input string
  • Create a function named divide of type NumberCalculator that divides the first number by the second number
  • Create a function named power of type NumberCalculator that raises the first number to the power of the second number (use Math.pow)
  • Create a function named isEmpty of type BooleanChecker that returns true if the string has length 0, otherwise false
  • Create a function named startsWithA of type BooleanChecker that returns true if the string starts with the letter "A" (case-sensitive), otherwise false

Test your functions by calling them with the following values and printing the results:

  • Call toUpperCase with "hello world"
  • Call addPrefix with "data"
  • Call divide with 20 and 4
  • Call power with 3 and 4
  • Call isEmpty with ""
  • Call startsWithA with "Apple"

Print each result on a separate line in the order specified above.

Cheat sheet

TypeScript allows you to create a type alias for a function's signature using the type keyword. The syntax shows parameter types in parentheses followed by an arrow and the return type:

type MathOperation = (a: number, b: number) => number;

const add: MathOperation = (a, b) => {
  return a + b;
};

const multiply: MathOperation = (a, b) => {
  return a * b;
};

Function type aliases act like blueprints that functions must match, ensuring consistency across your codebase.

Try it yourself

// TODO: Create type aliases here
// StringProcessor, NumberCalculator, BooleanChecker

// TODO: Implement the functions here
// toUpperCase, addPrefix, divide, power, isEmpty, startsWithA

// TODO: Test your functions and print the results
// Call each function with the specified values and print the results
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction To TypeScript