Introduction
In this article will learn the basic types - any & object type in detail. By functionality we will see the differences between them as well.
What is Any?
- Typescript is static type checking.
- User is not aware about the data type; then user can use any type.
- User can assign any datatype value to the variable, which is intitialized later.
- User can use any keyword to declare the datatype at the time of variable declaration.
- Suppose our function resturns a value which depends on condition & we are assigning this value to a variable; then we can define that the variable has any type.
Example
In the below example first we will declare a variable having type any. We will check the type of variable in the fucntion getValue() & according to that will return a string value from function.
- let myVariable: any;
- function getValue(myVariable): string {
- if (typeof(myVariable) === "number") {
- return "Variable is of number type & value is: " + myVariable;
- } else if (typeof(myVariable) === "string") {
- return "Variable is of string type & value is: " + myVariable;
- } else if (typeof(myVariable) == "boolean") {
- return "Variable is of boolean type & value is: " + myVariable;
- } else {
- return "Not able to trace the type of variable & value is: " + myVariable;
- }
- }
- console.log(getValue(5));
- console.log(getValue("Hello!!!"));
- console.log(getValue(true));
- let myArray: number[] = [10, 20, 30, , 40];
- console.log(getValue(myArray));

Sourav Kumar DasPosted Dec 26, 2019, 12:43 AM
Nice and useful article. Thanks for sharing.