AI News Hub Logo

AI News Hub

TypeScript Survival Guide (Part 2): Start Thinking in TypeScript

DEV Community
Noriuki

Thinking in TypeScript (Instead of Just Adding Types) When most developers start using TypeScript, they treat it like JavaScript with extra syntax. They add types… but don't really change how they think. And that's where most of the benefits are lost. In JavaScript, understanding a piece of code often requires reading everything carefully. In TypeScript, types make things explicit. type User = { name: string age: number } Now you don't need to guess what a User looks like. Types act as documentation. They help you (and others) understand the code faster. Instead of just adding types to variables, think about the shape of your data. Example: type Status = "loading" | "success" | "error" Now your code becomes more predictable. You are not just writing code — you are defining possible states. TypeScript can understand your code based on conditions. function print(value: string | number) { if (typeof value === "string") { console.log(value.toUpperCase()) } else { console.log(value.toFixed(2)) } } Even though value has multiple possible types, TypeScript knows exactly what it is inside each block. This is called type narrowing. And this is where TypeScript becomes truly useful. Using TypeScript is not just about avoiding errors. It's about writing code that is easier to understand, safer to use, and more predictable. Once you start thinking in types, you stop fighting TypeScript — and start using it properly.