Declaration consumer tests
Concept and boundary
Compile a consumer against distributed declarations in isolation from the implementation source. This tests the exported type contract actually available to consumers, including re-export paths. The fixture builds a library first and checks a separate consumer against its generated dist declaration.
Accepted project
Create a fresh folder with exactly these files. Use TypeScript 5.8.3 and Node.js 22.16.0. Package stubs under node_modules are local teaching fixtures, not installed dependencies.
import {twice} from "../lib/dist/index"; export const result=twice(3);consumer/tsconfig.json
{ "compilerOptions": { "composite": true, "strict": true, "outDir": "dist", "types": [] }, "files": [ "main.ts" ], "references": [ { "path": "../lib" } ]}lib/tsconfig.json
{ "compilerOptions": { "composite": true, "strict": true, "outDir": "dist", "types": [] }, "files": [ "index.ts" ]}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 -b consumer --pretty false.
Compilation succeeds (exit 0).
No runtime execution is required for this check.
lib/dist/index.d.ts exists.
Contains "twice(n: number): number".
consumer/dist/main.d.ts exists.
Contains "result: number".
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 {twice} from "../lib/dist/index"; export const result=twice("3");From the project root run: tsc -b consumer --pretty false.
Expected compiler diagnostic: TS2345
No runtime execution is required for this check.
The generated library declaration accepts number rather than string. Restore the accepted configuration/source to compare the result.
Practice and recap
Add an exported optional parameter and verify both old and new consumer calls.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Declaration consumer tests — knowledge check
Check the concept, interpret the tested project result, and diagnose a rejected configuration or source.
Which statement correctly describes Declaration consumer tests?
Using the complete accepted project and the exact command in the lesson, which result is expected?
Why does the rejected project fail under its stated configuration?