Any detection and test escapes
Concept and boundary
Assignments to expected types can pass when an API accidentally returns any. The IsAny pattern detects any by testing whether 0 extends the intersection of 1 and T. Use it alongside useful inference tests, not as a complete proof of safety.
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.
type IsAny<T>=0 extends (1&T)?true:false; type Assert<T extends false>=T; function read(){return 3;} type Test=Assert<IsAny<ReturnType<typeof read>>>; console.log(read());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:
3Rejected project — diagnosis
Intentionally rejected variant. Do not execute it.
Use a fresh copy of the accepted project, changing only the files shown below.
type IsAny<T>=0 extends (1&T)?true:false; type Assert<T extends false>=T; function read():any{return 3;} type Test=Assert<IsAny<ReturnType<typeof read>>>;From the project root run: tsc -p tsconfig.json --pretty false.
Expected compiler diagnostic: TS2344
No runtime execution is required for this check.
The weakened any return makes IsAny true, violating the false constraint. Restore the accepted configuration/source to compare the result.
Practice and recap
Test unknown with the same helper and explain the distinction.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Any detection and test escapes — knowledge check
Check the concept, interpret the tested project result, and diagnose a rejected configuration or source.
Which statement correctly describes Any detection and test escapes?
Using the complete accepted project and the exact command in the lesson, which result is expected?
type IsAny<T>=0 extends (1&T)?true:false; type Assert<T extends false>=T; function read(){return 3;} type Test=Assert<IsAny<ReturnType<typeof read>>>; console.log(read());Why does the rejected project fail under its stated configuration?
type IsAny<T>=0 extends (1&T)?true:false; type Assert<T extends false>=T; function read():any{return 3;} type Test=Assert<IsAny<ReturnType<typeof read>>>;