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.
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
// the `: number` here specifies that this function returns a number
function getTime(): number {
return new Date().getTime();
}
Void Return Type
Use the type void to say that a function does not return any value.
Example
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
function printHello(): void {
console.log('Hello!');
}
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
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
function multiply(a: number, b: number) {
return a * b;
}
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.
?.
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
// the `?` operator here marks parameter `c` as optional
function add(a: number, b: number, c?: number) {
return a + b + (c || 0);
}
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.
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
function pow(value: number, exponent: number = 10) {
return value ** exponent;
}
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
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
function divide({ dividend, divisor }: { dividend: number, divisor: number }) {
return dividend / divisor;
}
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.
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
function add(a: number, b: number, ...rest: number[]) {
return a + b + rest.reduce((p, c) => p + c, 0);
}
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.
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
type Negate = (value: number) => number;
// in this function, the parameter `value` automatically gets assigned the type `number` from the type `Negate`
const negateFunction: Negate = (value) => value * -1;