Data Type Classification

Rumman Ansari   Software Engineer   2023-12-05 06:13:52   60  Share
Subject Syllabus DetailsSubject Details 1 Program
☰ TContent
☰Fullscreen

Table of Content:

Data Type Classification

In TypeScript, data types can be classified in three ways:

any

It implies that the variable can take any type of value, which denotes a dynamic type.

    var value: any = 30;  //can take string, number, boolean anything

Built-in

Built-in types in Typescript are numberstringbooleannullundefined, and void.

 var value: string = "john"; //can take only string values

Difference between null and undefined

  • null - variable is set to an object whose value is undefined (empty)
  • undefined - variable has no value or object assigned to it.

User-defined

User-defined types include enumsclassesinterfacesarrays etc.

 interface ToDo {
  name: string;
  completed?: boolean; 
  // ? tells that 'completed' is optional property, user need not give value
 }
 let todo: ToDo = {
  name: 'some string'; 
 }
Getting Dirty With TypeScript Code

Let us look at some example codes written in TypeScript:

  • Let us start TypeScript with conventional "Hello World" program as shown below.
 class Greeter{
  greeting : T;
  constructor (public greeting : T) { this.greeting = greeting; }
  greet() {
   return "<h1>" + this.greeting + "</h1>";  
  }
 }
 var greeter = new Greeter <string>("Hello world");
 document.body.innerHTML = greeter.greet();
  • Following is a function to calculate Volume Of Cuboid and display result in web page.
  function volumeOfCuboid(length: number, width: number, height: number){
   var volume= length*width*height;
   return "Volume of the Cuboid is: " + volume;
  }
  document.body.innerHTML = volumeOfCuboid(1,2,3);
Generics

Generics are templates allowing the same function to accept arguments of various different types.

Generics create the components that are capable of working over data of present and future. It gives us the most dynamic capabilities for developing real time large scale software systems. C# and Java use generics enabling us to create a component that can work over a variety of types rather than a single one.

  • You can implement Generics by writing "T" instead of any specific data type like- number, boolean etc. as shown below.
 function calVolumeCube<T>(side: T): T {
     return side*side*side;
 }
  • To explicitly define type of arguments in function call, you can use <> (angular brackets) as shown below.

let volume = calVolumeCube <number> (5);

  • Else compiler will decide the type based on argument you pass as shown below.

let volume = calVolumeCube (5);

  • "T" is used to capture type sent by user.
Generics VS any

Let us consider an example.

    function identity<T>(arg: T): T {
        return arg;
    }

While any might look like generic, as that accepts any and all types of values for variable "arg", we are actually losing the information about what that type was when the function returns. If we pass in a number, the only information we have is that any type could be returned.

In contrast, Generics knows which type of data is to be returned.

Enums

Enum is a way to organize set of related values. Enum members have numeric value associated with them and can be either constant or computed. By default first member of enum is assigned value 0 (zero). Then each subsequent member is assigned value incremented by 1 automatically.

    enum CardSuit {
        Clubs,
        Diamonds,
        Hearts,
        Spades
    }
    // Sample usage
    var card = CardSuit.Clubs;
    // Safety
    card = "some string"; // Error : string is not assignable to type CardSuit