Friday, December 31, 2021

Typescript: 5 ways to declare a function

 There are different ways to write a function in typescript


//Method 1

function func0(age: number) {

  console.log(age);

}

func0(0);


//Method 2

const func1 = (age: number) => {

  console.log(age);

};

func1(1);


//Method 3

const func2 = function (age: number): void {

  console.log(age);

};

func2(2);


//Method 4

const func3: (age: number) => void = function (age: number): void {

  console.log(age);

};

func3(3);


//Method 5

const func4: (age: number) => void = (age: number): void => {

  console.log(age);

};

func4(4);


No comments: