Typescript

Datatypes

 

  • TypeScript provides a rich set of data types that allow developers to specify the type of data a variable can hold.
  • This feature improves code quality and maintainability by catching compile-time errors.
  • Below are the primary data types in TypeScript, explained in detail with coding examples.
  1. Number

    • Description: Represents both integers and floating-point numbers.
    • Example:
      let age: number = 40;        // Integer
      let price: number = 29.99;   // Floating-point
      let hex: number = 0xf00d;    // Hexadecimal
      let binary: number = 0b1010;  // Binary
      let octal: number = 0o744;    // Octal
  2. String

    • Description: Used for text data, it can be enclosed in single quotes, double quotes or backslashes (template literals).
    • Example
      let name: string = "John Doe";
      let greeting: string = `Hello, ${name}!`; // Template literal
  3. Boolean

    • Description: Represents a boolean value that can be true or false.
    • Example:
      let isActive: boolean = true;
  4. Null

    • Description: Represents the intentional absence of any object value.
    • Example:
      let user: null = null;
  5. Undefined

    • Description: Indicates the value assigned to an uninitialized variable.
    • Example:
      let uninitialized: undefined;
  6. Any

    • Description: A flexible type that allows any value and effectively disables type checking for this variable.
    • Example:
      let variable: any = "Hello";
      variable = 50; // Still valid
  7. Unknown

    • Description: A safer alternative to everything where you can’t perform operations on it until you’ve type checked it.
    • Example:
      let value: unknown = 5;
      // console.log(value.toFixed(2)); // Error: Object is of type 'unknown'
      if (typeof value === "number") {
         console.log(value.toFixed(2)); // Now it's safe to use
      }
  8. Void

    • Description: Used for functions that do not return a value.
    • Example:
      function logMessage(message: string): void {
         console.log(message);
      }
  9. Never

    • Description: Represents a type that never occurs, such as a function that always throws an error or a function with an infinite loop.
    • Example:
      function throwError(message: string): never {
         throw new Error(message);
      }
      function infiniteLoop(): never {
         while (true) {}
      }
  10. Object

    • Description: Represents a non-primitive type that can contain various properties.
    • Example:
      let person: object = {
         name: "John",
         age: 25,
         isActive: true,
      };
  11. Array

    • Description: It can contain a collection of values ​​of a specific type.
    • Example:
      let numbers: number[] = [1, 2, 3, 4];
      let strings: Array<string> = ["apple", "banana", "cherry"];
  12. Tuple

    • Description: Represents an array with a fixed number of elements, each of which can be of a different type.
    • Example:
      let tuple: [string, number] = ["John Doe", 20];
  13. Enum

    • Description: A method of assigning descriptive names to a set of numeric values.
    • Example:
      enum Courses { TypeScript, Ionic, Angular2, NodeJS };
      let tscourse: Courses = Courses.TypeScript;
      console.log(tscourse); // Outputs: 0 (the index of TypeScript)

Conclusion

  • Understanding these data types is critical to writing effective TypeScript code.
  • By leveraging static typing and various built-in types, developers can create more reliable and maintainable applications while catching bugs early in the development process.