Soundness boundaries
Concept and syntax
TypeScript deliberately permits some operations that are not fully sound, such as any and some structural assignments. Static success is not a proof that arbitrary runtime values match their declared types. At a trust boundary use unknown and check the needed facts; do not substitute type assertions for validation.
Working example
Save each snippet separately as main.ts in a fresh directory. Compile with tsc --strict --target ES2022 --module commonjs --moduleDetection force --skipLibCheck --noEmitOnError main.ts, then run the accepted example with node main.js. Forced module detection isolates snippet names from global declarations.
The accepted example deliberately exposes a static-safety limitation. Do not imitate the unsafe path in production.
const unchecked:any="bad";const n:number=unchecked;console.log(typeof n);function safe(x:unknown){return typeof x==="number"?x+1:0;}console.log(safe("bad"),safe(3));Expected output:
string0 4Common mistake
Intentionally invalid; compile separately and do not execute.
Expected diagnostic: TS18046. Unknown cannot be used as a number without evidence. Narrow it with typeof.
Practice and recap
Find a permitted unsafe array write and replace the consumer contract with readonly.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Soundness boundaries — knowledge check
Check the contract, trace output, and diagnose an invalid example.
Which statement about this lesson’s concept is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
const unchecked:any="bad";const n:number=unchecked;console.log(typeof n);function safe(x:unknown){return typeof x==="number"?x+1:0;}console.log(safe("bad"),safe(3));Which diagnostic and explanation match the rejected example under the lesson settings?
function safe(x:unknown){return x+1;}