Type System
A type system is a set of rules that assigns types to values and expressions, and uses those types to detect potential errors. Static type systems check types at compile time (TypeScript, Java); dynamic type systems check at runtime (JavaScript, Python).
Explanation
Types classify values: a number is different from a string, which is different from a function. Type systems use these classifications to catch errors before they cause runtime crashes. A static type system means the compiler knows the type of every expression before the program runs — if you try to call .toUpperCase() on a number, the compiler rejects it at build time, before you ship.
JavaScript is dynamically typed: variables don't have types, values do. A variable can hold a string, then a number, then an object. Type errors manifest at runtime as TypeErrors and silent undefined values. TypeScript adds optional static typing on top of JavaScript: you annotate variables and function signatures with types, and the TypeScript compiler checks consistency. TypeScript compiles to plain JavaScript — the type information is erased at runtime.
Type inference: you don't need to annotate every variable. const x = 42 — TypeScript knows x is a number without you writing const x: number = 42. Type inference means TypeScript adds value without adding verbosity. Generics (T in Array
Code Example
javascript// TypeScript type system fundamentals
// Basic types
let name: string = 'Alice';
let age: number = 30;
let active: boolean = true;
let data: unknown = fetchData(); // unknown: must narrow before use
// Union types
type ID = string | number;
function findUser(id: ID) { /* ... */ }
// Type inference: no annotation needed
const doubled = [1, 2, 3].map(n => n * 2); // number[]
// Generics: reusable type-safe containers
function first(arr: T[]): T | undefined { return arr[0]; }
const str = first(['a', 'b']); // type: string | undefined
const num = first([1, 2, 3]); // type: number | undefined
// Narrowing: TypeScript tracks runtime checks
function processId(id: string | number) {
if (typeof id === 'string') {
return id.toUpperCase(); // TS knows id is string here
}
return id.toFixed(2); // TS knows id is number here
}
// Utility types
type PartialUser = Partial; // all User fields optional
type ReadonlyUser = Readonly; // all fields readonly
type UserPreview = Pick; // subset of fields
Why It Matters for Engineers
TypeScript is now the dominant language for serious JavaScript development — used at Microsoft, Google, Airbnb, Slack, and virtually every large JavaScript codebase. Understanding the type system isn't optional; it's table stakes for professional JavaScript development. Types are documentation that's always in sync with the code: a function signature with TypeScript types tells you exactly what it accepts and returns, without reading the implementation. This dramatically reduces the cognitive load of working in a large codebase and catches entire classes of bugs (undefined property access, wrong argument types, missing null checks) before they reach production.
Related Terms
Interface · Class · Function · Immutability
Learn This In Practice
Go deeper with the full module on Beyond Vibe Code.
Programming Foundations → →