Use unknown for External Data (APIs, JSON)

1 min read

unknown is safer than any.
It forces you to validate the type before using it.

Bad (API response)

const response: any = await fetchData();
response.toUpperCase(); // may crash

Good

const response: unknown = await fetchData();

if (typeof response === "string") {
  response.toUpperCase(); // safe
}

More on TypeScript

Recent Tips

Validate on Backend. Always.

Frontend validation improves user experience. It shows instant errors and prevents obvious mistakes like empty fields or invalid email formats. But frontend validation is never security. Anyone can bypass it using DevTools, Postman, or by directly calling your API. That’s why backend validation is mandatory it protects your database and business logic from invalid or malicious data.

Use unknown for External Data (APIs, JSON) | thehardik