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
- id: C1 claim: The handbook has a Discriminated unions section. source: S1 evidence: "Discriminated unions"
- id: C2 claim: A kind field can tell circle and square variants apart. source: S2 evidence: "We'll use a field called
kindto tell which"
- id: C3 claim: Circles keep track of their radiuses in the example. source: S2 evidence: "Circles keep track of their radiuses and squares keep track of their side lengths."
- id: C4 claim: Most earlier narrowing examples use simple types such as string. source: S2 evidence: "narrowing single variables with simple types like
string"
Sources
- [S1] Narrowing (Discriminated unions). TypeScript Handbook. https://www.typescriptlang.org/docs/handbook/2/narrowing.html . License: CC-BY-4.0. Accessed 2026-11-16.
- [S2] Narrowing.md (TypeScript-Website). microsoft/TypeScript-Website (GitHub). https://github.com/microsoft/TypeScript-Website/blob/v2/packages/documentation/copy/en/handbook-v2/Narrowing.md . License: CC-BY-4.0. Accessed 2026-11-16.
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
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.},
}
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.