Structural assignability
Concept and syntax
TypeScript usually compares object types by their required members rather than their declared names. A value with additional fields can be assigned to a narrower structural contract through a variable. Fresh object literals have additional excess-property checks; they do not make object types globally exact.
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.
type Small={id:number};const full={id:7,label:"tea"};const small:Small=full;console.log(small.id);Expected output:
7Common mistake
Intentionally invalid; compile separately and do not execute.
type Small={id:number};const full={label:"tea"};const small:Small=full;Expected diagnostic: TS2741. The source lacks the required id member. Supply it with the right type.
Practice and recap
Assign an object with an extra field through a variable and then as a fresh literal.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Structural assignability — 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)?
type Small={id:number};const full={id:7,label:"tea"};const small:Small=full;console.log(small.id);Which diagnostic and explanation match the rejected example under the lesson settings?
type Small={id:number};const full={label:"tea"};const small:Small=full;