Explicit Types

Duration: 10 min•Difficulty: Easy•

Di TypeScript, kita bisa menentukan tipe data secara eksplisit menggunakan tanda titik dua (:).

Common Types

  • string
  • number
  • boolean
  • any (Hindari ini! Ini mematikan fitur pengecekan TypeScript).
main.ts
const userName: string = "Raffi";
const age: number = 20;
const isDeveloper: boolean = true;
// TypeScript akan marah jika Anda mencoba melakukan ini:
// userName = 123; // Error: Type 'number' is not assignable to type 'string'.
console.log(`User: ${userName}, Age: ${age}`);