Explicit Types

Duration: 10 min  •  Difficulty: Easy

In TypeScript, we can specify data types explicitly using a colon (:).

Common Types

  • string
  • number
  • boolean
  • any (Avoid this! It disables TypeScript's safety features).
  • main.ts
    const userName: string = "Raffi";
    const age: number = 20;
    const isDeveloper: boolean = true;
    // TypeScript will complain if you try to do this:
    // userName = 123; // Error: Type 'number' is not assignable to type 'string'.
    console.log(`User: ${userName}, Age: ${age}`);