TypeScript has a function type. Not a function, but an actual datatype representing a function. Now, consider our previous example of a add function.
also, consider the greet function:
Now, if we want to assign both add() and greet function to a single variable. We can do it like this:
Now, if we try to compile this:
$ tsc getCity.ts
$
The compiler did not complain. But, we know that greet() function and add() function are clearly different. This is because they both take different number of input parameters and return a different number of return types.
To be able to set a function type, we need a function type. This is so that, we can combine all add() related functions having the same input parameters and return types. To be able to fix the add() function to a datatype, let us see the following:
In this case, we are saying, the function myAdd()has two input parameters and both are numbers. Also, we have set its return type to be a number. So, now if we assign both add() and greet() function to myAdd() function and try to compile:
On compiling this time:
$ tsc getCity.ts
getCity.ts:29:1 - error TS2322: Type '() => void' is not assignable to type '(input1: number, input2: number) => number'.
Type 'void' is not assignable to type 'number'.
29 myAdd = greet;
~~~~~
getCity.ts:31:1 - error TS2554: Expected 2 arguments, but got 0.
31 myAdd();
~~~~~~~
getCity.ts:26:13
26 let myAdd: (input1:number, input2:number) => number;
~~~~~~~~~~~~~
An argument for 'input1' was not provided.
$
.. the compiler complains. This is because myAdd() is now a function type. To be able to make the compiler to compile. We can remove associating greet() function with myAdd().
$ tsc getCity.ts
$
Now, the compiler has compiled the code as myAdd() function type is associated with the correct function.
// function add to check for datatypes of arguments
function add(input1:number,input2:number):number{
var myResult = input1 + input2;
return myResult;
}
also, consider the greet function:
//void type
function greet():void{
console.log("Greetings! Welcome to getCity.ts");
}
Now, if we want to assign both add() and greet function to a single variable. We can do it like this:
let myAdd;
myAdd = add;
console.log("calling myAdd() == "+myAdd(2,3));
myAdd = greet;
console.log("calling myAdd() == ");
myAdd();
Now, if we try to compile this:
$ tsc getCity.ts
$
The compiler did not complain. But, we know that greet() function and add() function are clearly different. This is because they both take different number of input parameters and return a different number of return types.
To be able to set a function type, we need a function type. This is so that, we can combine all add() related functions having the same input parameters and return types. To be able to fix the add() function to a datatype, let us see the following:
// function types
let myAdd: (input1:number, input2:number) => number;
In this case, we are saying, the function myAdd()has two input parameters and both are numbers. Also, we have set its return type to be a number. So, now if we assign both add() and greet() function to myAdd() function and try to compile:
// function types
let myAdd: (input1:number, input2:number) => number;
myAdd = add;
console.log("calling myAdd() == "+myAdd(2,3));
myAdd = greet;
console.log("calling myAdd() == ");
myAdd();
On compiling this time:
$ tsc getCity.ts
getCity.ts:29:1 - error TS2322: Type '() => void' is not assignable to type '(input1: number, input2: number) => number'.
Type 'void' is not assignable to type 'number'.
29 myAdd = greet;
~~~~~
getCity.ts:31:1 - error TS2554: Expected 2 arguments, but got 0.
31 myAdd();
~~~~~~~
getCity.ts:26:13
26 let myAdd: (input1:number, input2:number) => number;
~~~~~~~~~~~~~
An argument for 'input1' was not provided.
$
.. the compiler complains. This is because myAdd() is now a function type. To be able to make the compiler to compile. We can remove associating greet() function with myAdd().
// function types
let myAdd: (input1:number, input2:number) => number;
myAdd = add;
console.log("calling myAdd() == "+myAdd(2,3));
$ tsc getCity.ts
$
Now, the compiler has compiled the code as myAdd() function type is associated with the correct function.
No comments:
Post a Comment