Skip to content

Typing Functions, Objects and API Data in TypeScript: Callbacks, Utility Types and fetch

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

You now know the building blocks: basic types, object shapes, unions and generics. This lesson puts them to work on the code you write every day in a web page. Functions take optional settings and callbacks. Objects get created, updated and shown in slightly different forms, and you do not want to write a new interface for each form. Data arrives from a server, and TypeScript cannot see what is really inside. And the page itself is full of elements and events whose types TypeScript already knows, if you ask the right way. By the end you can type a small real feature from the input box to the API call without a single any.

Quick answer

Type each parameter and, for exported functions, the return type. Mark optional parameters with ?, give defaults with = and collect extras with ...rest: number[]. Name callback shapes with a function type such as type Formatter = (n: number) => string. Build variants of an object type with utility types instead of copying it: Partial<T> makes every property optional, Pick<T, "a" | "b"> keeps some, Omit<T, "id"> drops some, Readonly<T> blocks changes and Record<K, V> maps keys to values. An async function returns Promise<T>. Treat JSON from fetch as unknown and check it before use. For elements, use querySelector<HTMLInputElement> or an instanceof check.

40 min read and practice · Content checked on 17 November 2026

In this lesson you will

  • Type optional, default and rest parameters, and callbacks with function types
  • Derive object types with Partial, Pick, Omit, Readonly and Record
  • Type async functions and check JSON from an API before trusting it
  • Get the right element and event types from the DOM

Parameters, return types and callbacks

Function parameters are where types pay off most, because they are the contract other code relies on. A parameter that may be left out gets ?, as in greet(name?: string), and inside the function its type includes undefined. A parameter with a default, rate = 0.05, is optional for the caller and its type is inferred from the default. A rest parameter, ...prices: number[], collects any number of extra arguments into an array. A function that returns nothing useful has the return type void.

Functions are values, so they have types too. A callback that turns an amount into display text has the type (amount: number) => string. Give it a name with type Formatter = (amount: number) => string; and use it for variables and parameters. When you assign an arrow function to a Formatter variable, you can leave out the parameter types: TypeScript takes them from the declared type. This is contextual typing, and it is why prices.map((p) => p * 2) needs no annotation either. Write return types on functions other files use; inside a file, inference is usually enough.

Try it: Defaults, rest parameters and a Formatter callback
Code
ResultUpdates as you type
Console
  1. Console messages, errors, form data and clicked links appear here.

The two formatters need no parameter types because Formatter supplies them. The last two lines are rejected: "100" is a string, and a callback that expects a string does not fit Formatter. Remove them and the Console reports no type errors.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 3 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

How do I type the JSON response from fetch in TypeScript?

Declare an interface for the data you expect, store await response.json() as unknown, and check it with a type guard or a validation library before returning it as that interface. Annotating the result directly compiles but checks nothing.

What is the difference between Pick and Omit in TypeScript?

Pick<T, Keys> keeps only the listed properties, and Omit<T, Keys> keeps everything except them. Use whichever list is shorter: Pick<Product, "name" | "price"> for a small view, Omit<Product, "id"> for "all but one".

What is the return type of an async function in TypeScript?

Always a Promise. If the function returns a number, its type is Promise<number>; if it returns nothing, Promise<void>. Writing a plain number as the return type of an async function is an error.

How do I get the value of an input in TypeScript?

Select it so TypeScript knows it is an input: document.querySelector<HTMLInputElement>("#qty"), or check el instanceof HTMLInputElement. Then check for null and read el.value, which is always a string, so convert it with Number() for numbers.

Should I write return types on every TypeScript function?

Not necessarily. Inference gets return types right, so small internal functions can skip them. Write them on exported functions and anything other files depend on: the declared type documents the contract and turns a wrong return into an error at the function, not at every caller.

Sources

Facts in this lesson were checked against these official pages on 17 November 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.