Menu

1 / 4

TypeScript Won’t Save You From Bad API Data

JavaScript in Plain English - Medium·Bestte·about 8 hours ago
#sfpAytyA
Reading 0:00
15s threshold

Member-only story

TypeScript Won’t Save You From Bad API Data

Bestte

4 min read7 hours ago

Your code compiles. Your types look perfect. Then the API returns something nobody expected.

Press enter or click to view image in full size

One of the best things about TypeScript is how much confidence it gives you.

You define an interface, write a function, and suddenly your editor knows what everything should look like.

Missing properties? Red underline.

Wrong argument types? Compiler error.

Accidentally treating a string like a number? TypeScript usually catches that too.

It’s easy to get comfortable with this.

Maybe a little too comfortable.

Because there’s one thing TypeScript doesn’t actually do: verify that the data arriving from your API matches the types you wrote.

And that’s where things get interesting.

The interface that lied

Imagine a simple user response:

interface User {

id: number;

name: string;

isAdmin: boolean;

}

Nothing unusual here.

Now we receive some JSON from an…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.