Skip to main content

TypeScript Functions

TypeScript has its own syntax for giving types to function parameters and return values.

This page builds on plain JavaScript functions, so make sure you know how those work first.


Return Type

You can state the type of the value a function returns by writing it after the parameter list.

Note: If you do not write a return type, TypeScript tries to infer it — it guesses the type from the values the function returns.

Example

The : number after the parentheses says getTime returns a number.

Prompt
Show how to declare a function's return type explicitly. Write a function getTime annotated to return number, whose body returns new Date().getTime(), with a comment explaining the : number annotation. The example only declares the function and prints nothing.
Output: TypeScript file and captured output

Void Return Type

Use the type void to say that a function does not return any value.

Example

Prompt
Show the void return type for a function that returns nothing. Write a function printHello annotated as returning void whose body logs 'Hello!'. Only declare the function; do not call it, so nothing is printed.
Output: TypeScript file and captured output

Parameters

Function parameters get types with the same syntax as variable declarations: a colon and the type after the name.

Note: If a parameter has no type, TypeScript falls back to any — unless it can find the type elsewhere, as shown in the Default Parameters and Type Alias sections below.

Example

Prompt
Show how function parameters get type annotations. Write a function multiply with two number parameters a and b that returns their product, letting TypeScript infer the return type. The example only declares the function and prints nothing.
Output: TypeScript file and captured output

Optional Parameters

By default, TypeScript treats every parameter as required. You can mark a parameter as optional by adding ? after its name.

Example

The ? makes c optional, so add can be called with two or three arguments.

Prompt
Show how to mark a function parameter as optional with ?. Write a function add with required number parameters a and b and an optional number parameter c, returning a + b + (c || 0), with a comment explaining the ?. The example only declares the function and prints nothing.
Output: TypeScript file and captured output

Default Parameters

For a parameter with a default value, write the default after the type annotation.

TypeScript can also infer the parameter's type from the default value alone.

Example

exponent is typed as number and defaults to 10 when no value is passed.

Prompt
Show a parameter with both a type annotation and a default value. Write a function pow with a number parameter value and a number parameter exponent that defaults to 10, returning value ** exponent. The example only declares the function and prints nothing.
Output: TypeScript file and captured output

Named Parameters

Named parameters — values pulled out of one object argument — are typed the same way as normal parameters: the type goes after the destructuring pattern.

Example

Prompt
Show how to type named (destructured) parameters. Write a function divide that destructures dividend and divisor from a single object parameter typed as { dividend: number, divisor: number } and returns dividend / divisor. The example only declares the function and prints nothing.
Output: TypeScript file and captured output

Rest Parameters

A rest parameter collects any extra arguments. It is typed like a normal parameter, but the type must be an array, because a rest parameter is always an array.

Example

...rest is typed number[], so every extra argument must be a number.

Prompt
Show how to type a rest parameter as an array. Write a function add with number parameters a and b plus a rest parameter ...rest typed number[], returning a + b plus the sum of rest computed with reduce. The example only declares the function and prints nothing.
Output: TypeScript file and captured output

Type Alias

A function type can be written separately from the function itself using a type alias.

These types look like arrow functions: the parameters in parentheses, then => and the return type.

Example

Because negateFunction is typed as Negate, its value parameter automatically gets the type number.

Prompt
Show a function type defined separately with a type alias. Declare a type alias Negate for a function taking a number and returning a number, then assign an arrow function (value) => value * -1 to a constant negateFunction of type Negate, with a comment noting that value gets its type from the alias. The example only declares the function and prints nothing.
Output: TypeScript file and captured output