Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

TypeScript function types

Function types in TypeScript 

TypeScript function types make it possible for programmers to express the expected signature of a function. A function signature is a sequence of parameter types plus a return type.


TypeScript function type sample code:


function MyVote(candidate: string, callback: (result: string) => any) { //here some code ... }
    MyVote("MyParty", function (result: string) { if (result === "MyParty") { //here ur code ... } } );

In detail:

A function’s type has the same two parts: the type of the arguments and the return type. When writing out the whole function type, both parts are required. We write out the parameter types just like a parameter list, giving each parameter a name and a type.

//simple js function

        function add(x, y) {
            return x + y;


 //typescript simple function type implementation

        function add(x: number, y: number): number {
            return x + y;
        let myAdd = function (x: number, y: number): number { return x + y; };



//Typescript proper function type use

      let myAdd: (baseValue: number, increment:
number) => number =
      function (x: number, y: number): number { return x + y; };