Why You Should Avoid Using any (and What to Do Instead)
any
is a special TypeScript type that tells the compiler:
"Trust me, I know what I'm doing."
When you use any
, TypeScript turns off type checking for that variable — which defeats the purpose of using TypeScript in the first place.
Why Overusing any is a Problem
The biggest benefit of TypeScript is catching bugs before they happen. any
disables that:
Plus, you miss out on one of the best developer experience features of TypeScript:
Helpful autocompletion and suggestions from your IDE!
What to Use Instead
Use unknown over any
If you truly don’t know the type yet, use unknown
. It forces you to do checks before using the value:
Use Partial, Pick, etc. for Flexibility
TypeScript utility types let you write more flexible types without giving up safety:
Ending notes
Rule of Thumb:
If you’re using
any
, ask yourself: “What am I losing in type safety by doing this?”
Last updated