We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Function Type Syntax

One of the most useful places for explicit types is in function signatures. For example:

function createMessage(name: string, a: number, b: number): string {
  return `${name} scored ${a + b}`;
}

The : type after each parameter specifies that parameter's type, and the : type after all the parameters specifies the return type. It works the same way with arrow functions:

const createMessage = (name: string, a: number, b: number): string => {
  return `${name} scored ${a + b}`;
};

Assignment

We need to calculate discounts for Support.ai customers. Run the function as-is, and notice that tsc is showing us some compile-time errors.

Fix the calculateTotal function by using the proper types. It should accept three parameters:

  • price: a number representing the base price
  • quantity: a number representing how many support chats they've purchased
  • discount: a number representing discount percentage (e.g., 0.1 for 10%)

Then return the total price (a number) after applying the discount.