Complete TypeScript application
Concept and boundary
Combine transport decoding, a domain service, typed repository contracts and a thin entry point. Test a valid request, a malformed request and a domain rejection using an in-memory repository. Emit declarations for the service boundary. This is a local end-to-end teaching application, not a deployed service.
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).
import {makeService,MemoryRepo} from "./service"; const repo=new MemoryRepo(); const create=makeService(repo); console.log(create({id:"a",qty:2}),create({id:"a",qty:3}),create({id:"b",qty:"1"}),repo.count());export type Item=Readonly<{id:string;qty:number}>; export interface Repo{exists(id:string):boolean;save(item:Item):void} export class MemoryRepo implements Repo{private rows=new Map<string,Item>();exists(id:string){return this.rows.has(id)} save(item:Item){this.rows.set(item.id,item)} count(){return this.rows.size}} export function makeService(repo:Repo){return (raw:unknown):"created"|"duplicate"|"invalid"=>{if(typeof raw!=="object"||raw===null||!("id" in raw)||!("qty" in raw)||typeof raw.id!=="string"||typeof raw.qty!=="number"||!Number.isSafeInteger(raw.qty)||raw.qty<1)return "invalid";if(repo.exists(raw.id))return "duplicate";repo.save({id:raw.id,qty:raw.qty});return "created"}}tsconfig.json
{ "compilerOptions": { "strict": true, "target": "ES2022", "module": "CommonJS", "moduleResolution": "Node10", "moduleDetection": "force", "skipLibCheck": true, "noEmitOnError": true, "types": [], "declaration": true, "outDir": "dist" }, "include": [ "**/*" ], "exclude": [ "node_modules", "dist", "build", "cache" ]}From the project root run: tsc -p tsconfig.json --pretty false.
Compilation succeeds (exit 0).
Run node dist/main.js. Expected stdout:
created duplicate invalid 1dist/service.d.ts exists.
Contains "interface Repo".
Contains "makeService".
Rejected project — diagnosis
Intentionally rejected variant. Do not execute it.
Use a fresh copy of the accepted project, changing only the files shown below.
import {makeService} from "./service"; makeService({save(){return true}});From the project root run: tsc -p tsconfig.json --pretty false.
Expected compiler diagnostic: TS2345
No runtime execution is required for this check.
A repository must implement both exists and save. Restore the accepted configuration/source to compare the result.
Practice and recap
Add a read operation, an explicit Result type, repository failure handling and a second consumer test.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Complete TypeScript application — knowledge check
Check the concept, interpret the tested project result, and diagnose a rejected configuration or source.
Which statement correctly describes Complete TypeScript application?
Using the complete accepted project and the exact command in the lesson, which result is expected?
import {makeService,MemoryRepo} from "./service"; const repo=new MemoryRepo(); const create=makeService(repo); console.log(create({id:"a",qty:2}),create({id:"a",qty:3}),create({id:"b",qty:"1"}),repo.count());Why does the rejected project fail under its stated configuration?
import {makeService} from "./service"; makeService({save(){return true}});