Albert Em’s Post

"any" isn't a type. It's you giving up. You write any and TypeScript stops helping you. Completely. It won't catch typos. It won't catch wrong methods. It won't catch anything. You basically paid the TypeScript tax (extra syntax, build step, config files) and got nothing back. With any: call any method, access any property, TypeScript says nothing. Code explodes at runtime. With unknown: TypeScript blocks you until you prove what it is. ts const data = parseJSON(str) // unknown 👏 if (typeof data === 'object' && data && 'name' in data) { console.log(data.name) // now it's safe } "But that's more code" Yes. That's the point. That code is the check you should have written anyway. TypeScript just forced you to not forget it. When to use unknown: → API responses → JSON parsing → User input → Any external data you don't control When to use any: → Never → Ok maybe when migrating JS to TS → But then fix it immediately Every any in your codebase is a bug waiting to happen. You just haven't found it yet. #typescript #javascript #frontend #webdev #programming #webdevelopment #react #cleancode #devtips #softwaredevelopment

  • text

To view or add a comment, sign in

Explore content categories