Negative diagnostic tests
Concept and boundary
Compile invalid source separately and check the expected diagnostic code and source location. A nonzero exit alone could mean a missing module or syntax error rather than the intended failure. This fixture uses a two-line consumer with a mismatch on line two.
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.
tsconfig.json
{ "compilerOptions": { "strict": true, "target": "ES2022", "module": "CommonJS", "moduleResolution": "Node10", "moduleDetection": "force", "skipLibCheck": true, "noEmitOnError": true, "types": [], "noEmit": true }, "include": [ "**/*" ], "exclude": [ "node_modules", "dist", "build", "cache" ]}From the project root run: tsc -p tsconfig.json --pretty false.
Compilation succeeds (exit 0).
No runtime execution is required for this check.
Rejected project — diagnosis
Intentionally rejected variant. Do not execute it.
Use a fresh copy of the accepted project, changing only the files shown below.
From the project root run: tsc -p tsconfig.json --pretty false.
Expected compiler diagnostic: TS2322
No runtime execution is required for this check.
Compiler output contains "main.ts(2,7): error TS2322".
The mismatch is TS2322 at the second line, not a syntax error. Restore the accepted configuration/source to compare the result.
Practice and recap
Change the second line to a missing name and compare the code and position.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Negative diagnostic tests — knowledge check
Check the concept, interpret the tested project result, and diagnose a rejected configuration or source.
Which statement correctly describes Negative diagnostic tests?
Using the complete accepted project and the exact command in the lesson, which result is expected?
const n:number=1;const s:string=String(n);Why does the rejected project fail under its stated configuration?
const n:number=1;const s:string=n;