Intersection types
Concept and syntax
An intersection A & B requires a value to satisfy both contracts at once. For compatible object types this means all required properties are present. An intersection is a type operation, not a runtime merge; construct the combined object yourself.
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 RecordInfo = {id:number} & {label:string};const item:RecordInfo = {id:2,label:"tea"};console.log(item.id,item.label);Expected output:
2 teaCommon mistake
Intentionally invalid; compile separately and do not execute.
type RecordInfo = {id:number} & {label:string};const item:RecordInfo = {id:2};Expected diagnostic: TS2322. The label is required by the second contract. Supply it.
Practice and recap
Intersect a timestamp contract with RecordInfo.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Intersection types — 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 RecordInfo = {id:number} & {label:string};const item:RecordInfo = {id:2,label:"tea"};console.log(item.id,item.label);Which diagnostic and explanation match the rejected example under the lesson settings?
type RecordInfo = {id:number} & {label:string};const item:RecordInfo = {id:2};