Open code, practice, and notes →
Download source with English comments · for reading
TypeScript · 20 Conditional Types and Distribution
Series contents · Technology map
Stage: Validation and Type Transformations · Editable example · Storage ID: typescript-step-20
Goals and learning outcomes
- Record example result types for the three inputs.
- Compare accepted and rejected assignments with and without distribution.
Prerequisites
Work and experiment records from Unit 19, “State Tables with Mapped Types”
Key concepts and explanations
Type Conditions
Conditional types select a result based on type relationships. They operate separately from runtime if statements.
Union Distribution
A condition on a naked type parameter may apply to each union member. Wrapping it in a tuple controls distribution.
Practice setup
First check the execution scope guidance on the example screen. The following environment is for extending the assignment in a separate project. Implement it yourself in a separate local project and check it with browser developer tools and tsc --noEmit. Record the installed tsc version in a separate TypeScript project. Enable strict in tsconfig.json and check with tsc --noEmit, then open the runtime exercise using compiled output or a local development server. Do not assume the browser executes TS type annotations directly.
The linked example is a starting point. It does not implement every task below. Complete work beyond its supported scope in a separate project.
Practice tasks
- In conditional.ts, prepare conditions for a string array, a number, and a union of the two types.
- Create array-element extraction and distributive and non-distributive helper types, and check results through assignment.
- Run with a string array, a number, and a union of the two types. Results to check: Record example result types for the three inputs. Compare accepted and rejected assignments with and without distribution. Record expected and actual results separately.
Variables to observe
| Variable | Baseline | Change | Expected change / hypothesis |
|---|---|---|---|
| Condition Form | T extends condition | tuple-wrapped condition | Pre-run hypothesis: the result of testing the union as a whole may differ. Keep other conditions fixed and record the actual result separately after running. |
Predictions in this table are not measured results. Change one condition at a time and record why the actual result differs.
Completion criteria
- Record example result types for the three inputs.
- Compare accepted and rejected assignments with and without distribution.
Self-check
- Do conditional types reduce execution time?
- When is distribution unwanted?
Official references
Linked example and execution scope
Example source · Runtime environments and limits
The preview is a JavaScript runtime boundary experiment. It does not run TypeScript diagnostics. Check lesson.ts locally with tsc --strict --noEmit --lib ES2023,DOM.
Source code and records are not overwritten automatically. Resetting an existing example also preserves your notes.
Next learning
Previous: State Tables with Mapped Types · Next: Template Literal Event Names