DEV Community

Birusha Ndegeya
Birusha Ndegeya

Posted on

TypeScript Function


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

  1. Return Type
function getTime(): number  {
   return new Date().getTime();

// with arrow function
const getFullYear = (): number => {
   return new Date().getFullYear();

const time: number = getTime();
const year: number = getFullYear();
Enter fullscreen mode Exit fullscreen mode
  1. Void Return Type

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

function main (): void {
   console.log('Hello world');

// arrow functions

const bye = (): void => {
   console.log('Good bye! Crual World!');

Enter fullscreen mode Exit fullscreen mode
  1. Parameters

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

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

// arrow function

const mult = (a: number, b: number): number => a *b;

const product: number = mult(148, 48);
const prod: number = multiply(5, 6);

Enter fullscreen mode Exit fullscreen mode

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.

  1. Optional Parameters

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

function add (a: number, b: number, c?:number): number {
   return a + b + (c || 0);

// Arrow function

const addition = (a: number, b:number, c?:number): number => a + b + (c || 0);

const s: number = add(10, 38);
const sum: number = addition(12, 48, 39);

Enter fullscreen mode Exit fullscreen mode
  1. Default Parameters

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

function pow (value: number, exponent: number = 10): number {
   return value ** exponent;

// arrow function

const power = (value: number, exponent: number = 2): number => value ** exponent;

const resp: number = power(24, 3);
const result: number = pow(20);
Enter fullscreen mode Exit fullscreen mode

TypeScript can also infer the type from the default value.

  1. Named Parameters

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

function divise({ dividend, divisor}: {dividend: number, divisor: number}): number{
   return dividend / divisor;

console.log(divise({ dividend: 20, divisor: 4}));

// or we can do this - long way

type ParameterType = {
   dividend: number;
   divisor: number;

const diviseN = ({ dividend, divisor }: ParameterType): number => dividend / divisor;

const args: ParameterType = {
   dividend: 204,
   divisor: 24,

Enter fullscreen mode Exit fullscreen mode
  1. Rest Parameters

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

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

// arrow function

const addition = (a:number, b: number, number[]): number => {
   return a + b + rest.reduce((ac: number, cv:number) => ac + cv, 0);
const arr = [1, 2, 3];
console.log(add(5, 3, ...arr));
console.log(addition(2, 3, ...arr));
Enter fullscreen mode Exit fullscreen mode
  1. Type Alias

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

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) => -Math.abs(value);

Enter fullscreen mode Exit fullscreen mode

Top comments (0)