← All notes

TypeScript discriminated unions narrow a value by a shared kind field

Answer: Most earlier handbook examples have focused around narrowing single variables with simple types like string, boolean, and number. Discriminated unions cover more complex structures.

The shape example

Imagine encoding shapes like circles and squares. Circles keep track of their radiuses. Squares store a side length in the example. We'll use a field called kind to tell which shape is in use.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: TypeScript Handbook, Narrowing (Discriminated unions), CC-BY-4.0 (https://raw.githubusercontent.com/microsoft/TypeScript-Website/v2/LICENSE); microsoft/TypeScript-Website (GitHub), Narrowing.md (TypeScript-Website), CC-BY-4.0 (https://raw.githubusercontent.com/microsoft/TypeScript-Website/v2/LICENSE). Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: TypeScript Handbook, Narrowing (Discriminated unions), CC-BY-4.0 (https://raw.githubusercontent.com/microsoft/TypeScript-Website/v2/LICENSE); microsoft/TypeScript-Website (GitHub), Narrowing.md (TypeScript-Website), CC-BY-4.0 (https://raw.githubusercontent.com/microsoft/TypeScript-Website/v2/LICENSE).

Cite this

Permanent ID: iil:note:1583ptgq

iinventedlight. 2026. "TypeScript discriminated unions narrow a value by a shared kind field." iinventedlight Library, iil:note:1583ptgq, version of 2026-11-16. https://iinventedlight.com/library/notes/typescript-discriminated-unions/. License: CC-BY-4.0.

@misc{iil_note_1583ptgq,
  author = {{iinventedlight}},
  title = {{TypeScript discriminated unions narrow a value by a shared kind field}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-16},
  url = {https://iinventedlight.com/library/notes/typescript-discriminated-unions/},
  note = {Permanent ID iil:note:1583ptgq. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/typescript-discriminated-unions.md.