Validated API client
Concept and boundary
Design the client around a transport returning unknown and a decoder returning a discriminated Result. The mock transport supplies valid and invalid payloads without network variability. Transport failures are a distinct branch.
Accepted project
Create a fresh folder with exactly these files. Use TypeScript 5.8.3 and Node.js 22.16.0. For the AST explorer only, install the pinned typescript package locally (npm install --save-dev typescript@5.8.3).
export type User={id:number}; export type Result={ok:true;value:User}|{ok:false;error:"invalid"|"transport"}; export async function load(request:()=>Promise<unknown>):Promise<Result>{let raw:unknown;try{raw=await request()}catch{return {ok:false,error:"transport"}} if(typeof raw==="object"&&raw!==null&&"id" in raw&&typeof raw.id==="number"&&Number.isSafeInteger(raw.id))return {ok:true,value:{id:raw.id}};return {ok:false,error:"invalid"}}import {load} from "./client"; async function main(){for(const request of [async()=>({id:3}),async()=>({id:"3"}),async()=>{throw Error("offline")}]){const r=await load(request);console.log(r.ok?r.value.id:r.error)}} void main();tsconfig.json
{ "compilerOptions": { "strict": true, "target": "ES2022", "module": "CommonJS", "moduleResolution": "Node10", "moduleDetection": "force", "skipLibCheck": true, "noEmitOnError": true, "types": [] }, "include": [ "**/*" ], "exclude": [ "node_modules", "dist", "build", "cache" ]}From the project root run: tsc -p tsconfig.json --pretty false.
Compilation succeeds (exit 0).
Run node main.js. Expected stdout:
3invalidtransportRejected project — diagnosis
Intentionally rejected variant. Do not execute it.
Use a fresh copy of the accepted project, changing only the files shown below.
import type {User} from "./client"; const raw:unknown={id:"3"}; const u:User=raw;From the project root run: tsc -p tsconfig.json --pretty false.
Expected compiler diagnostic: TS2322
No runtime execution is required for this check.
Unknown data cannot be assigned directly to User. Restore the accepted configuration/source to compare the result.
Practice and recap
Add an optional name field and test valid, missing and malformed names with mocked requests.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Validated API client — knowledge check
Check the concept, interpret the tested project result, and diagnose a rejected configuration or source.
Which statement correctly describes Validated API client?
Using the complete accepted project and the exact command in the lesson, which result is expected?
import {load} from "./client"; async function main(){for(const request of [async()=>({id:3}),async()=>({id:"3"}),async()=>{throw Error("offline")}]){const r=await load(request);console.log(r.ok?r.value.id:r.error)}} void main();Why does the rejected project fail under its stated configuration?
import type {User} from "./client"; const raw:unknown={id:"3"}; const u:User=raw;