Runtime guards and decoders
Concept and boundary
A decoder checks null, object membership and the member type, then returns a typed result. In this example only a record with numeric count is accepted; additional properties are ignored. A predicate is a promise from its implementation to the checker.
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 Item={count:number}; function decode(x:unknown):Item|undefined{if(typeof x==="object"&&x!==null&&"count" in x&&typeof x.count==="number")return {count:x.count};} console.log(decode({count:3})?.count,decode({count:"3"}));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:
3 undefinedRejected project — diagnosis
Intentionally rejected variant. Do not execute it.
Use a fresh copy of the accepted project, changing only the files shown below.
type Item={count:number}; function decode(x:unknown):Item{return x;}From the project root run: tsc -p tsconfig.json --pretty false.
Expected compiler diagnostic: TS2322
No runtime execution is required for this check.
Returning an unchecked unknown violates Item. Restore the accepted configuration/source to compare the result.
Independent comparison: reject-null-and-string
The decoder explicitly rejects null and string-valued members, rather than coercing them.
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 Item={count:number}; function decode(x:unknown):Item|undefined{if(typeof x==="object"&&x!==null&&"count" in x&&typeof x.count==="number")return {count:x.count};} console.log(decode(null),decode({count:"3"}));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:
undefined undefinedPractice and recap
Reject NaN too using Number.isFinite after the type check.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Runtime guards and decoders — knowledge check
Check the concept, interpret the tested project result, and diagnose a rejected configuration or source.
Which statement correctly describes Runtime guards and decoders?
Using the complete accepted project and the exact command in the lesson, which result is expected?
type Item={count:number}; function decode(x:unknown):Item|undefined{if(typeof x==="object"&&x!==null&&"count" in x&&typeof x.count==="number")return {count:x.count};} console.log(decode({count:3})?.count,decode({count:"3"}));Why does the rejected project fail under its stated configuration?
type Item={count:number}; function decode(x:unknown):Item{return x;}What is the result of the independent reject-null-and-string comparison?
type Item={count:number}; function decode(x:unknown):Item|undefined{if(typeof x==="object"&&x!==null&&"count" in x&&typeof x.count==="number")return {count:x.count};} console.log(decode(null),decode({count:"3"}));