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.
Console
- 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
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.
- TypeScript Handbook: More on Functions
- TypeScript Handbook: Utility Types
- TypeScript Handbook: DOM Manipulation
- MDN: Response.json()
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.