Tutorials Videos Menu
Website Pro NEW

TypeScript Functions


TypeScript has a specific syntax for typing function parameters and return values.

Read more about functions here.


Return Type

The type of the value returned by the function can be explicitly defined.

Example

// the `: number` here specifies that this function returns a number
function getTime(): number {
  return new Date().getTime();
}
Try it Yourself »

If no return type is defined, TypeScript will attempt to infer it through the types of the variables or expressions returned.


Void Return Type

The type void can be used to indicate a function doesn't return any value.

Example

function printHello(): void {
  console.log('Hello!');
}

Try it Yourself »

Parameters

Function parameters are typed with a similar syntax as variable declarations.

Example

function multiply(a: number, b: number) {
  return a * b;
}
Try it Yourself »

If no parameter type is defined, TypeScript will default to using any, unless additional type information is available as shown in the Default Parameters and Type Alias sections below.