Introduction
A variable is a uniquely named location used to store data in any programming language. In Typescript, the variable must be declared before being used, using the keyword let or var. The syntax for declaring variables in Typescript is a little different from other languages and looks something like this,
var [variable_name] : [type_of_variable] = [value] ;
The variable may or may not have a value and type initialized.
For example
var name: string = “Ninja”;
var name: string;
var name = “Ninja”;
var name;
All of these are correct methods of initializing a variable.
In Typescript, it is not always necessary to explicitly provide the type of a variable. Typescript holds power to infer the type of a variable on its own. This automatic Inference by the compiler is known as Type Inference.
Type Inference occurs,
- At the time of variable initialization.
- While default values are set for the parameters.
- At the time when function return types are determined.
For example,
var check_num = 123;
var check_name = "Ninja"
console.log("Type of variable check_name " + typeof(check_name));
console.log("Type of variable check_num " + typeof(check_num));
In this example, we have not specified the variable types. The Typescript compiler automatically infers the type of variables declared.
What if we tried to assign the check_name to check_num?
var check_num = 123;
var check_name = "Ninja"
check_num = check_name;
We get an error saying the string type cannot be assigned to the number type.
Inference in Complex Objects
Type Inference can be helpful in complex objects, like arrays, having values of different data types. In such cases, the compiler automatically infers the array type by either considering a super data type or viewing the union of two or more data types.
For example, Type Inference in an array will look like this,
var array = ["Code","Ninja",null,"Hey"];
The Inference algorithm will consider the above array and all the values and infer its type as string and null. So if we try and add a number type value to it, the compiler will show an error.
var array = ["Code","Ninja",null,"Hey"];
array.push(10);
We tried to push a number value in the array in this code snippet, but the compiler returned an error saying a number cannot be assigned to a string array.
Let us take another example,
var array = ["Code","Ninja",10,"Hey",20];
In this array, we have added both number and string type so the Inference algorithm will infer the type of the array as the union of string and number. So if we try and add a number to this array, the compiler will not throw an error.
var array = ["Code","Ninja",10,"Hey",20];
array.push(40);
console.log(array);
We tried to push a number to the array, and since it's a union of string and number, the compiler successfully added the number to the collection without any error.
Let us take yet another example and consider Type Inference in the case of functions,
function power(base:number,pow:number){
var final = 1;
var i = 1;
for(i;i<=pow;i++){
final = final*base;
}
return final;
}
var str:string = power(10,2);
console.log(str);
In this example, we have created a function to return the value of a number raised to a user-specified power. The Inference algorithm infers the function's return type to be a number. So, if we try and store it in a string variable, the compiler will throw an error.
Though if we stored it in a number variable, the compiler would not throw any error
function power(base:number,pow:number){
var final = 1;
var i = 1;
for(i;i<=pow;i++){
final = final*base;
}
return final;
}
var num:number = power(10,2);
console.log("The output of the function is "+num);