Data Types in TypeScript


TypeScript provides several data types to work with. Here are the main types:

# Primitive Types

1. **Boolean**

   ```typescript
   let isDone: boolean = false;
  1. Number

     let decimal: number = 6;
     let hex: number = 0xf00d;
     let binary: number = 0b1010;
     let octal: number = 0o744;
    
  2. String

     let color: string = "blue";
     color = "red";
    
  3. Array

     let list: number[] = [1, 2, 3];
     let list: Array<number> = [1, 2, 3];
    
  4. Tuple

     let x: [string, number];
     x = ["hello", 10];
    
  5. Enum

     enum Color {
       Red,
       Green,
       Blue,
     }
     let c: Color = Color.Green;
    
  6. Any

     let notSure: any = 4;
     notSure = "maybe a string instead";
     notSure = false;
    
  7. Void

     function warnUser(): void {
       console.log("This is my warning message");
     }
    
  8. Null and Undefined

     let u: undefined = undefined;
     let n: null = null;
    
  9. Never

    function error(message: string): never {
      throw new Error(message);
    }
    
  10. Object

    let obj: object = { name: "John", age: 30 };
    

These types help in building robust and type-safe applications in TypeScript.

0
Subscribe to my newsletter

Read articles from Soumya Ranjan Mahanta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Soumya Ranjan Mahanta
Soumya Ranjan Mahanta