Skip to content
Back to Knowledge Hub

TypeScript practice

Does TypeScript validate data returned by an API?

TypeScript checks code during development; a type assertion does not inspect an API response at runtime. Treat external input as unknown and check its shape before using it.

By Alhaji C. M. Kallon · · intermediate · 4 min with practice

Before you start

  • Understand JavaScript objects and arrays
  • Read basic TypeScript types

What you will practise

  • Separate static types from runtime checks
  • Narrow unknown values safely

Quick answer

TypeScript checks code during development; a type assertion does not inspect an API response at runtime. Treat external input as unknown and check its shape before using it.

Build a small example

function readTitle(value: unknown): string | null {
  if (typeof value !== 'object' || value === null) return null;
  if (!('title' in value)) return null;
  return typeof value.title === 'string' ? value.title : null;
}

This checks only the title field. A production parser needs to validate every field the application relies on, including meaningful limits and allowed values.

Try it yourself

Pass null, an empty object, a numeric title, an empty string and a valid title. Decide whether an empty title should be accepted for your feature. Encode that decision explicitly.

Common mistake

Writing “as Project” can silence a compiler error while leaving malformed data untouched. Type assertions are statements to the compiler, not runtime evidence.

Check your understanding

Why check null separately? JavaScript reports typeof null as object. A broad object check alone cannot establish that reading a property is safe.

Next step

Apply the same boundary thinking to form input and API loading states.

Sources and further reading

Published 2 November 2026. Examples are teaching exercises, not claims about client results.