liveBook · Manning

Union type

As explained in Acing the Frontend Interview

A union type allows a value or parameter to have one of several specified types.

A union type lets a value or function parameter use one of several specified types. It fits limited, predictable cases such as a parameter accepting a string or number. section 3.8

Use union types when evaluating a function-parameter design:

  • Choose a union when the allowed types are a fixed, known set and no relationship between input and output is required. section 3.8
  • Account for type narrowing: unions can be less specific about return types and may require additional narrowing. section 3.8
  • Prefer generics when the function must support open-ended types or preserve a strong input-output relationship; prefer unions when simplicity matters more than reusability. section 3.8

How Acing the Frontend Interview covers it

How deep it goes
Compares unions with generics and notes narrowing, but stops short of a worked union implementation.
Examples
Uses a function parameter accepting a string or number as the representative fixed-set example.
What you can do after
Evaluate function-parameter designs and choose unions or generics according to type-set size, relationships, and simplicity.
How it connects
Connects union types to generics, type narrowing, input-output relationships, simplicity, flexibility, and type safety.

Also explained in

Compare how 13 books explain union type →

Related concepts

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