Open code, practice, and notes →
Download source with English comments · for reading
TypeScript · 19 State Tables with Mapped Types
Series contents · Technology map
Stage: Validation and Type Transformations · Editable example · Storage ID: typescript-step-19
Goals and learning outcomes
- Every configuration key has a corresponding message.
- Record the missing-new-key diagnostic and the corrected result.
Prerequisites
Work and experiment records from Unit 18, “Checking Configuration with satisfies”
Key concepts and explanations
Iterating Keys
A Mapped Type maps each member of a key union to a type. It does not generate a runtime loop.
Modifiers
You can add or remove readonly and optional modifiers. Check that the derived contract matches actual usage policy.
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 mapped.ts, prepare conditions for a configuration with 3 keys and the addition of 1 new key.
- Write an error-message map for configuration keys and a read-only configuration type.
- Run with a configuration with 3 keys and the addition of 1 new key. Results to check: Every configuration key has a corresponding message. Record the missing-new-key diagnostic and the corrected result. Record expected and actual results separately.
Variables to observe
| Variable | Baseline | Change | Expected change / hypothesis |
|---|---|---|---|
| Configuration Key Count | 3 keys | 4 keys | Pre-run hypothesis: a missing required map entry will be revealed. 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
- Every configuration key has a corresponding message.
- Record the missing-new-key diagnostic and the corrected result.
Self-check
- Does a Mapped Type create an actual object?
- Can an optional map detect missing entries?
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: Checking Configuration with satisfies · Next: Conditional Types and Distribution