liveBook · Manning

Type information

As explained in React in Depth

Type information is metadata specifying the kinds of values that variables, parameters, expressions, or return values may contain.

Type information specifies the kinds of values that variables, parameters, expressions, or return values may contain. In TypeScript, annotations give the checker enough information to validate function calls and implementations, while inference supplies types that are already evident. section 5.2.2

Function parameters are a common boundary where annotations matter. Without types, TypeScript cannot reliably infer a function's return type and may report implicit any. Adding number annotations makes invalid calls fail while allowing valid results and downstream variables to be inferred. section 5.2.2

Use explicit type information selectively:

  • Annotate function arguments when their types are not available from context. section 5.2.2
  • Let TypeScript infer literals, arithmetic results, built-in method results, and values returned by typed functions. section 5.2.2
  • Represent alternatives with unions, such as null|string, and restrict fixed states with literal unions such as LOADING|SUCCESS|ERROR. section 5.2.2
  • Describe React component props with either a type alias or interface, including optional properties and defaults. section 5.2.2

Typed memoized React callback

When a callback moves outside an inline rendering context to support useCallback, contextual inference is lost. Annotate its curried arguments directly: (index: number) => (evt: MouseEvent<HTMLButtonElement>) => void. Equivalent placements include a useCallback type argument or an explicit target-variable type. The playlist example uses this callback to dispatch typed reorder actions while memoizing the handler. section 6.1.6

function fibonacci(a:number, b:number) {
  return a + b;
}

Compare an untyped Fibonacci function with explicit number annotations, then inspect the resulting call-site validation. section 5.2.2

Listing 6.6 The playlist with a memoized callback
import { useReorderable } from "./useReorderable";
import "./playlist.css";
import { ActionType, Song } from "./types";
import { PlaylistItem } from "./PlaylistItem";
import { MouseEvent, useCallback } from "react";
interface PlaylistProps {
  songs: Song[];
}
export function Playlist({ songs }: PlaylistProps) {
  const [list, dispatch] = useReorderable(songs);
  const handleMove = useCallback(
    (index: number) =>     #1
     (evt: MouseEvent<HTMLButtonElement>) => {   #1
      const type = evt.currentTarget.name as ActionType;
      dispatch({ type, index });
    },
    [dispatch]
  );
  return (
    <ol>
      {list.map((song, index) => (
        <PlaylistItem
          key={song.id}
          song={song}
          index={index}
          move={handleMove}
        />
      ))}
    </ol>
  );
}

Inspect a memoized React callback whose nested event-handler and index types preserve the playlist component's contract. section 6.1.6

How React in Depth covers it

How deep it goes
Explains parameter annotations, inference, unions, props, and callback typing, but stops short of broader TypeScript type-system internals.
Examples
Uses Fibonacci validation, nullable and status unions, typed Person props, and a memoized playlist-reordering callback.
What it assumes
Assumes JavaScript and React fundamentals while teaching explicit annotations, inference, unions, and props through working examples.
What you can do after
Apply it via TypeScript annotations and React callback patterns to catch invalid calls and preserve component contracts.
How it connects
Connects static typing with type inference, union types, React props, useCallback, memoization, and event types.

Also explained in

Compare how 2 books explain type information →

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.