๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐—ง๐˜†๐—ฝ๐—ฒ ๐—”๐—ฃ๐—œ ๐—ฅ๐—ฒ๐˜€๐—ฝ๐—ผ๐—ป๐˜€๐—ฒ๐˜€ ๐—ถ๐—ป ๐—ง๐˜†๐—ฝ๐—ฒ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜

TypeScript checks your code. It does not check the data you receive at runtime.

If a third-party API changes a number to a string, your app will crash. TypeScript will not warn you. This happens because your types are just a promise. No one enforces that promise once data crosses the network.

The Problem with Fetch

The fetch function returns data as any. Using any turns off type checking. You can access properties that do not exist. This leads to runtime crashes.

The Danger of Type Assertions

You might use the as keyword to tell the compiler to trust you.

const user = (await response.json()) as User;

This makes your editor happy. It provides autocomplete. But it does not verify the data. If the API returns a null value where you expect a string, your app fails in production. An assertion silences the compiler instead of fixing the problem.

A Better Way: Schema Validation

To fix this, you must check the data at the edge of your app. Use a library like Zod to create a schema.

A schema acts as your single source of truth. You write the schema once and derive your TypeScript type from it.

  1. Start with unknown: Use the unknown type for incoming JSON. This forces you to verify the data before use.
  2. Parse the data: Use your schema to inspect the payload.
  3. Catch errors early: If the data is wrong, the validator throws an error at the boundary. You find the bug immediately.

When to use different methods:

Stop lying to your compiler. Verify your data at the boundary.

Source: https://dev.to/hugonnaili/how-to-type-third-party-api-responses-in-typescript-without-lying-to-your-compiler-4cdn