Skip to content
Goal: Proficient✓ Sources verified 2026-11-24TypeScriptZodNarrowing

TypeScript Essentials (for Full-Stack AI Apps) ​

Core concepts ​

FeatureUse
Union types + narrowingNarrow a wide type to a specific one with if, in, typeof or a discriminant field
Discriminated unionsDistinguish several shapes by one shared field (such as type / status); ideal for modelling task states and SSE events
unknown vs anyTreat external data as unknown first and use it only after validation; any switches type checking off
GenericsWrite reusable API clients and table components
satisfiesChecks that a value matches a type while keeping the more precise inferred type
Utility typesPartial, Pick, Omit, Record, ReturnType, etc.

How it works: types exist only at compile time ​

TypeScript types are erased after compilation and nothing is checked at runtime. So data from the backend, an LLM or user input must be validated at runtime (e.g. with Zod) before you get a type you can trust.

In practice ​

Modelling task state with a discriminated union ​

ts
type Task =
  | { status: 'queued'; id: string }
  | { status: 'running'; id: string; step: string; progress: number }
  | { status: 'needs_review'; id: string; reason: string }
  | { status: 'done'; id: string; result: ContractFields }
  | { status: 'failed'; id: string; error: string; retryable: boolean }

function label(t: Task) {
  switch (t.status) {
    case 'running': return `${t.step} ${t.progress}%`   // step is accessible here
    case 'failed': return t.retryable ? 'Failed, retryable' : 'Failed'
    default: return t.status
  }
}

Runtime validation: Zod ​

ts
import { z } from 'zod'
const SseEvent = z.discriminatedUnion('type', [
  z.object({ type: z.literal('token'), text: z.string() }),
  z.object({ type: z.literal('done'), stop_reason: z.string() }),
])
const evt = SseEvent.parse(JSON.parse(data))   // throws if it doesn't match

Keeping frontend and backend types in sync ​

FastAPI generates OpenAPI docs automatically, so you can generate TS types from the OpenAPI spec with a tool such as openapi-typescript instead of writing them twice.

Common pitfalls ​

  1. Overusing as assertions: data as Task only fools the compiler; the runtime data may still be wrong.
  2. strict not enabled: new projects should turn on strict: true.
  3. Handling API responses as any: errors travel all the way to the UI before they surface.

Common interview questions ​

  1. What's the difference between unknown and any?
  2. What is a discriminated union? Give an example you've used.
  3. Can TypeScript guarantee type safety at runtime? How do you handle external data?
  4. What's the difference between interface and type?

Sources ​

Vin Amory · AI development × content creation · Technical notes cite official docs in each page’s sources section