Samir Telrandhe’s Post

🚫 any is TypeScript's escape hatch. And most of us use it way too often. Every time you type any, you're telling TypeScript: "Stop helping me here." Here are the 4 types I reach for instead 👇 1. unknown — the safe version of any When you don't know the type upfront (API responses, JSON parsing), use unknown. It forces you to narrow the type before using it. TypeScript keeps protecting you. 2. never — for exhaustive checks Use it in switch statements to catch unhandled cases at compile time. If you add a new union member and forget to handle it, TypeScript errors immediately — not in production. 3. Record<K, V> — for typed dictionaries Instead of { [key: string]: any }, write Record<string, User>. Now your values are typed and your IDE actually helps you. 4. Union types A | B — enumerate what's valid Don't loosen the type — enumerate exactly what's allowed. type Status = 'loading' | 'success' | 'error' is infinitely better than string. The rule I follow: → If the type is truly uncertain → unknown → If a case should never happen → never → If it's a key-value map → Record<K,V> → If it's one of N values → union type any has maybe 2 legitimate use cases. Everything else has a better option. Which of these do you find yourself using most? 👇 #TypeScript #JavaScript #WebDevelopment #CleanCode #Frontend

  • graphical user interface

To view or add a comment, sign in

Explore content categories