TypeScript Types Demystified: Simple Types, Special Types, and Type Inference
TypeScript Types Demystified: Simple Types, Special Types, and Type Inference In the first post , we covered why TypeScript exists and how to write your first program. Now it's time to get comfortable with the type system itself — the foundation everything else is built on. By the end of this post, you'll know how to type variables, arrays, and function parameters correctly. You'll also understand the "special" types that trip up most beginners: any , unknown , never , and void . The Core Primitive Types TypeScript's basic types map directly to JavaScript's primitives: // string let firstName : string = " Ramesh " ; let greeting : string = `Hello, ${ firstName } ` ; // number (no separate int/float — it's all number) let age : number = 31 ; let price : number = 9.99 ; let hex : number = 0xFF ; // boolean let isLoggedIn : boolean = true ; let hasAccess : boolean = false ; These are the types you'll use most often. Simple, predictable, and exactly what you'd expect. Type Inference: TypeScript Does the Work You don't always have to write the type. TypeScript infers it from the value you assign: let city = " Chennai " ; // TypeScript infers: string let year = 2026 ; // TypeScript infers: number let isActive = true ; // TypeScript infers: boolean Once inferred, that type is locked in: let city = " Chennai " ; city = 42 ; // ❌ Error: Type 'number' is not assignable to type 'string' Rule of thumb: Let TypeScript infer types for local variables. Write explicit annotations for function parameters and return types. // Let inference work for variables const scores = [ 95 , 87 , 72 ]; // inferred as number[] // Be explicit for function signatures function calculateAverage ( scores : number []): number { return scores . reduce (( a , b ) => a + b , 0 ) / scores . length ; } Explicit vs Inferred — When to Choose Each // ✅ Explicit annotation — good for function params & return types function formatName ( first : string , last : string ): string { return ` ${ first } ${ last } ` ;