Type Systems in TypeScript: A Comprehensive Guide
A type system is a set of rules the compiler uses to decide what's allowed and what isn't. In TypeScript, that checking happens at compile time — before y...
23 Apr 2024 · 2 min read

A type system is a set of rules the compiler uses to decide what's allowed and what isn't. In TypeScript, that checking happens at compile time — before your code ever runs. The goal: catch mistakes early instead of discovering them in production.
Type annotations
TypeScript can infer types, but sometimes you need to be explicit. That's a type annotation.
let name: string = 'John';
let age: number = 30;
You're telling the compiler: "This is a string. This is a number. Don't let me use them as anything else."
When to annotate vs. when to let TypeScript infer? I annotate function parameters and return types. I let inference handle local variables. That's the sweet spot between safety and readability.
Type guards
Sometimes a value could be one of several types. A type guard narrows it down within a specific scope.
Think of it as asking "are you a number?" and getting a compiler-verified answer.
function isNumber<T>(value: T): value is number {
return typeof value === 'number';
}
let age: number | string = 30;
if (isNumber(age)) {
console.log(`Age: ${age}`); // TypeScript knows age is a number here
} else {
console.log('Not a number');
}
The value is number return type is the key. It tells TypeScript to narrow the type inside the if block.
Conditional types
Conditional types let you create types that change based on a condition. They follow the same A extends B ? C : D pattern as a ternary expression, but at the type level.
type IsString<T> = T extends string ? string : never;
type NameType = IsString<"John">; // string
type AgeType = IsString<42>; // never
If T extends string, the type resolves to string. Otherwise, never.
This is powerful for building flexible utility types. But it's also where TypeScript's type system starts to feel like its own programming language. Use conditional types when you need them. Don't reach for them to be clever.
The trade-off
TypeScript's type system catches real bugs. I've seen it prevent production incidents that would have taken hours to debug.
The cost: it adds complexity. Overly clever types slow down your team more than they protect it. The best TypeScript code uses the simplest types that get the job done.
Ehsan Gazar helps software engineers take LLM and agent systems from demo to production. He writes about building LLM systems that hold up in production: evals, tracing, RAG, agent architecture and cost control. He teaches the Senior to Staff AI Engineer cohort on Maven, where engineers learn to design and defend these systems.
Forward Deployed AI Engineering: From Customer Problem to Production Value
A live Maven cohort for engineers: 10 live 90-minute sessions taking one AI engagement from customer problem to production, ending with eight artefacts and a working slice in your own repo.
January cohort starts Tue 12 Jan 2027, Tue & Thu 6:30pm London. Enrolling now, 20 seats, $1,800.
View the live cohortKeep reading
- TypeScript Anti-Patterns That Cost You Twice: Build Time, Runtime, and the Interview
- The Frontend Toolchain Is Now Written in Rust and Go. What That Means for You
- oxlint vs ESLint: Why I Replaced ESLint with oxlint
- SystemJS Is Dead. Native ESM Finally Won.
- Replace Axios with a Simple Custom Fetch Wrapper (Production-Ready Guide)
- Stop Shipping ChatGPT Wrappers. Ship an Agent in TypeScript, or Don't Bother.