Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

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.


w3schools CERTIFIED . 2022

Get Certified!

Complete the TypeScript modules, do the exercises, take the exam and become w3schools certified!!

$45 ENROLL

Optional Parameters

By default TypeScript will assume all parameters are required, but they can be explicitly marked as optional.

Example

// the `?` operator here marks parameter `c` as optional
function add(a: number, b: number, c?: number) {
  return a + b + (c || 0);
}
Try it Yourself »

Default Parameters

For parameters with default values, the default value goes after the type annotation:

Example

function pow(value: number, exponent: number = 10) {
  return value ** exponent;
}
Try it Yourself »

TypeScript can also infer the type from the default value.


Named Parameters

Typing named parameters follows the same pattern as typing normal parameters.

Example

function divide({ dividend, divisor }: { dividend: number, divisor: number }) {
  return dividend / divisor;
}
Try it Yourself »

Rest Parameters

Rest parameters can be typed like normal parameters, but the type must be an array as rest parameters are always arrays.

Example

function add(a: number, b: number, ...rest: number[]) {
  return a + b + rest.reduce((p, c) => p + c, 0);
}
Try it Yourself »

Type Alias

Function types can be specified separately from functions with type aliases.

These types are written similarly to arrow functions, read more about arrow functions here.

Example

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;
Try it Yourself »

TypeScript Exercises

Test Yourself With Exercises

Exercise:

Create a function that returns the string "Learning is Fun!", with the return type explicitly defined:

 myFunc():  {
   "Learning is Fun!";
}

Start the Exercise



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.