Basic Types & Type Annotations
Primitive Types
TypeScript provides static typing for primitive data types, ensuring type safety and preventing unintended type mismatches.
- `string`, `number`, `boolean`, `null`, `undefined`, `symbol`, and `bigint` are the core primitive types in TypeScript.
- These types help prevent runtime errors by enforcing correct data usage.
- Type inference allows implicit type assignment where the type is determined automatically.
// Explicit type annotations
let message: string = "Hello, TypeScript!";
let count: number = 42;
let isValid: boolean = true;
// Type inference (TypeScript automatically infers the type)
let city = "Sydney"; // Inferred as string
let score = 99; // Inferred as number