Open code, practice, and notes →
Download source with English comments · for reading
TypeScript · 14 Generics and Boundary Validation
Series contents · Technology map
Stage: Unions and Generics · Editable example · Storage ID: typescript-02
Goals and learning outcomes
- Record acceptance or rejection for all four inputs.
- Use Note fields on a valid return value without assertions.
Prerequisites
Work and experiment records from Unit 13, “Constraints and Minimal Contracts”
Key concepts and explanations
Storage Relationships
Type parameters express the type relationship between saving and loading. Stored strings must be validated again when loaded.
Structural Checks
Receive JSON.parse results as unknown and check them with a guard. Check null and fields inside arrays before trusting them.
Practice setup
The existing Frontory preview is a limited starting example, not a complete implementation of the assignment. Implement additional files and features yourself in a separate local project. 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 storage-boundary.ts, prepare conditions for a valid array, an empty array, null, and a numeric title.
- Create save and load that accepts a validation function, then connect a Note array guard.
- Run with a valid array, an empty array, null, and a numeric title. Results to check: Record acceptance or rejection for all four inputs. Use Note fields on a valid return value without assertions. Record expected and actual results separately.
Variables to observe
| Variable | Baseline | Change | Expected change / hypothesis |
|---|---|---|---|
| JSON Field | string title | numeric title | Pre-run hypothesis: the restored result will be classified as an error. 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 acceptance or rejection for all four inputs.
- Use Note fields on a valid return value without assertions.
Self-check
- Does generic T check JSON structure?
- Is an empty array valid under the contract?
Official references
Linked example and execution scope
Example source · Runtime environments and limits
Source code and records are not overwritten automatically. Resetting an existing example also preserves your notes.
Next learning
Previous: Constraints and Minimal Contracts · Next: Type Guards and False Guarantees