Four TypesScript code smells: 1
Four TypesScript code smells:
1. Using “any”
2. Using “as”
3. Not validating HTTP responses (via tools like Zod)
4. Setting “strict” to false
Why? Because the items above increase the risk of runtime errors.
#TypeScript
Some asked why avoid “as”?
Rule: Prefer type declarations over type assertions.
Why? Because assertions can lie. Using “as” to assert a type is saying to TS “trust me, I’m right”.
Sometimes, it’s necessary (to cast a DOM query for instance). But, if possible, declare a type.
Clarification: I’m not saying any of these are universally “wrong”.
That’s why I said “smell”, rather than “anti-pattern”.
When I see these things in TypeScript code, it’s often a bad sign, but not always.
So, I look closer and consider the context.