bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/TypeScript/TypeScript Core
TypeScript•TypeScript Core

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();
}

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!');
}

Parameters

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

Example

function multiply(a: number, b: number) {
  return a * b;
}

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.

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);
}

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;
}

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;
}

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);
}

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;

Previous

TypeScript Union Types

Next

TypeScript Casting

This chapter

Overview
40

Lessons

241m

Read time

1. TypeScript Tutorial2. TypeScript Introduction3. TypeScript Getting Started4. TypeScript Simple Types5. TypeScript Explicit Types and Inference6. TypeScript Special Types7. TypeScript Arrays8. TypeScript Tuples9. TypeScript Object Types10. TypeScript Enums11. TypeScript Type Aliases and Interfaces12. TypeScript Union Types13. TypeScript Functions14. TypeScript Casting15. TypeScript Classes16. TypeScript Basic Generics17. TypeScript Utility Types18. TypeScript Keyof19. TypeScript Null & Undefined20. TypeScript Definitely Typed21. TypeScript 5.x Updates22. TypeScript Configuration23. TypeScript with Node.js24. TypeScript with React25. TypeScript Tooling26. TypeScript Advanced Types27. TypeScript Type Guards28. TypeScript Conditional Types29. TypeScript Mapped Types30. TypeScript Type Inference31. TypeScript Literal Types32. TypeScript Namespaces33. TypeScript Index Signatures34. TypeScript Declaration Merging35. TypeScript Async Programming36. TypeScript Decorators37. TypeScript in JavaScript Projects (JSDoc)38. TypeScript Migration Guide39. TypeScript Error Handling40. TypeScript Best Practices

On this page

Return TypeVoid Return TypeParametersOptional ParametersDefault ParametersNamed ParametersRest ParametersType Alias