liveBook · Manning

Type argument

As explained in React in Depth

A type argument is an argument supplied to a parameterized construct to specify a type or constrain the kinds of values it accepts.

A type argument supplies a type to a generic function, interface, or component. In TypeScript, it appears in angle brackets, such as `getMiddle<number>([0, 1, 2])`. The generic function can then relate its input and output types without committing to one element type. Function calls often infer the argument from their values, so write it explicitly when inference is too narrow or unclear. section 5.3.1

function getMiddle<Type>(list: Type[]): Type {
  const mid = Math.floor(list.length / 2);
  return list[mid];
}

The same `Type` parameter describes both the array elements and the returned element. A number array therefore produces a number; a string array produces a string. section 5.3.1

An explicit argument can also make a type less precise. `getMiddle<number | string>([0, 1, 2])` has a `number | string` return type even though the supplied values are numbers. Prefer the inferred type when it already captures the intended relationship. section 5.3.1

In React, supply a type argument where an initial value or an API signature cannot express the intended type:

  • `useState<null | boolean>(null)` permits an initially undecided value to become a Boolean; `useState<string[]>([])` permits strings to be added to an initially empty array. section 6.1.1
  • `createContext<DarkMode | null>(null)` allows a later provider value to contain the expected object. `useContext` then receives its type from that context without another argument. section 6.1.3
  • `forwardRef<HTMLInputElement, InputProps>(...)` identifies the ref element first and component props second. Typing the callback’s ref as `Ref<HTMLInputElement>` is another option; choose one approach consistently. section 5.3.8
  • `PropsWithChildren<HeadingProps>` adds children to named props. Its empty-props default is available in React 18 and later; React 17 and earlier require an explicit argument for children-only props. section 5.3.2

One pagination component, different item types

Define `PaginableProps<Type>` with `items: Type[]` and a renderer whose `item` is `Type`. A generic `Paginable<Type>` can then paginate employee cards or high-score entries while keeping each renderer aligned with its items. Watch the boundary: memoizing or forwarding a ref to a generic component can cause its type argument to be forgotten and requires a workaround. section 6.2 section 6.5

How React in Depth covers it

How deep it goes
Works through inference, explicit unions, hook typing, and ref-argument order; flags generic-component wrappers without detailing their workaround.
Examples
Uses a middle-element function, nullable appointment state, an empty tag array, typed refs, and pagination over two item types.
What it assumes
Assumes familiarity with React APIs and TypeScript annotations while introducing generic function syntax through an array example.
What you can do after
Apply it via TypeScript and React to preserve intended types across generic functions, hooks, contexts, refs, and components.
How it connects
Connects generic functions and components to type inference, union types, React state, context, props, and refs.

Also explained in

Compare how 5 books explain type argument →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React in Depth. The section links point to the parts of the book it draws on.