Type Annotations & Inference TS
Add explicit types and leverage TypeScript's type inference
// Basic annotations
let count: number = 0;
let title: string = 'Hello TS';
let isDone: boolean = false;
// Type inference (TS infers the type)
const maxItems = 10; // number
let message = 'Hi'; // string
// Function parameter + return type
function add(a: number, b: number): number {
return a + b;
}
// Object type
const user: { id: number; name: string } = {
id: 1,
name: 'Alice'
};