Basic Types - Any And Object In Typescript

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.
  1. let myVariable: any;  
  2.   
  3. function getValue(myVariable): string {  
  4.     if (typeof(myVariable) === "number") {  
  5.         return "Variable is of number type & value is: " + myVariable;  
  6.     } else if (typeof(myVariable) === "string") {  
  7.         return "Variable is of string type & value is: " + myVariable;  
  8.     } else if (typeof(myVariable) == "boolean") {  
  9.         return "Variable is of boolean type & value is: " + myVariable;  
  10.     } else {  
  11.         return "Not able to trace the type of variable & value is: " + myVariable;  
  12.     }  
  13. }  
  14. console.log(getValue(5));  
  15. console.log(getValue("Hello!!!"));  
  16. console.log(getValue(true));  
  17. let myArray: number[] = [10, 20, 30, , 40];  
  18. console.log(getValue(myArray));  
Output 
 
Execute the code & you will get output like,
  • Variable is of number type & value is: 5
  • Variable is of string type & value is: Hello!!!
  • Variable is of boolean type & value is: true
  • Not able to trace the type of variable & value is: 10,20,30,,40

What is Object?

  • Object refers to the JavaScript object type.
  • It is non-primitive type.
  • Object appears to be a more specific declaration than any.
  • Every object has a life & user can access the only members inside that objects.
  • It may contains properties & functions.
Example
 
Consider the below example, as we have declared the two variables; i.e., first has any type & second has object. We wiill access the methods from both the variables.
  1. let a: any;  
  2. let b: Object;  
  3. console.log(a.method()); // It will transpiles without error  
  4. console.log(b.method()); // It will give error 'method' does not exist on type 'Object' or will get console error Cannot read property 'method' of undefined  
Example
 
Consider the below example variable is declare as object & we are accessing the properties & method from that.
  1. let myObject: object;  
  2. myObject = {  
  3.     a: 10,  
  4.     b: 20,  
  5.     getAddition: function(): number {  
  6.         return this.a + this.b;  
  7.     }  
  8. };  
  9. console.log("Function call & addition is: " + myObject.getAddition());   
Output
 
Execute the code & we will get output like:
 
Function call & addition is: 30
 

Summary

 
In this article, you learned basic types - any & object in TypeScript.