Typing Parameters & Return Values

You can type function parameters like this:

function total(numbers: number[]) {
  return numbers.reduce((a, b) => a + b)
}

total([/**/"string1"/**/])
Type 'string' is not assignable to type 'number'.

Here, we type the numbers parameter to be an array of numbers. So when we pass an array of strings, we get an array.

Also, we can type the return value of a function:

function introduce(
  firstName: string,
  lastName: string
): string {
  /**/return/**/ 500
}
Type 'number' is not assignable to type 'string'.

By putting : type after the parentheses (), we declare the type that this function should return. So when we return a number from the function, we get an error.