GOTCHA NOTE #029
Every `any` Is a Bug Waiting
The problem:
const data: any = fetchFromAPI();data.email; // TypeScript doesn't check this. Crash if email doesn't exist.You type-checked everything else. Then you hit something unknown and use any. It feels safe.
It’s not. any is “I’m opting out of type safety here.”
The fix: Use unknown
const data: unknown = fetchFromAPI();// data.email; // ✗ Error: unknown doesn't have email property
const typed = data as { email: string }; // ✓ Intentional casttyped.email; // Now safeOr parse with zod:
const userSchema = z.object({ email: z.string() });const data = userSchema.parse(fetchFromAPI());data.email; // Type-safe, validatedEnforce with eslint:
{ "rules": { "@typescript-eslint/no-explicit-any": "error" }}If you must use any, leave a comment:
// TODO: type this properly once the API schema is finalizedconst data: any = fetchFromAPI();Every any is a TODO. Close them before shipping.