TypeScript examples

The TypeScript hub holds examples where the type system is part of the answer rather than decoration. You will find narrowing of unknown JSON, discriminated unions for API results, generic request wrappers, typed React props and context, and validation rules expressed as types the compiler can check. Every entry explains which TypeScript version features it assumes, shows the exact declaration order that compiles, and warns about the places where a cast would hide a real runtime risk. Browse the task groups to find the boundary you need, then read the explanation to understand why the compiler accepts the solution and when it should reject a variant.

7 published examples in 4 taskgroups. Hub pages list every Example in exactly one task group.

Fetch API data

1 example

Send a request, await the response, and handle status, timeout, and cancellation behaviour.

Manage React state

2 examples

Declare, derive, update, and share component state inside the React render model.

  • Memoise a derived value with typed dependencies

    Give a memoised derived value a typed dependency list and a stable key function, and see why dependency identity rather than the memo call decides whether the cache is ever reused.

    TypeScriptintermediateReact

  • Type a React context provider and its consumer

    Type a React context with a null sentinel, expose a provider and a hook, and turn a missing provider into one named error instead of a screen full of undefined props.

    TypeScriptintermediateReact

Transform data

3 examples

Reshape collections and records in memory into the structure the next step needs.

Validate form input

1 example

Decide whether entered data is acceptable and report the result to the person filling the form.

  • Type the validation state of a controlled form

    Key field errors and touched flags off the form's own field names, so a renamed input cannot keep an orphaned message and the compiler lists every field left unhandled.

    TypeScriptadvancedReact

Adjacent navigation