MediumUnion & Intersection💻 Output Question

Intersection type — must satisfy all merged properties

💡

Hint

Intersection types require ALL properties from all intersected types. When the same property appears with incompatible types, TypeScript resolves it to never — making it unusable. At runtime, object spread takes the last value.

What does this output?

// Simulate TypeScript intersection type A & B
// An intersection must have ALL properties from both types

function merge(a, b) {
  return { ...a, ...b };
}

// Serializable = { serialize(): string }
// Loggable     = { log(): void }
// Combined     = Serializable & Loggable = must have both

const serializable = { serialize: () => '{"name":"Alice"}' };
const loggable = { log: () => console.log('logged') };

const combined = merge(serializable, loggable);

// combined has ALL properties from both
console.log(typeof combined.serialize);
console.log(typeof combined.log);
console.log(combined.serialize());

// Check what happens when same key appears (last writer wins at runtime)
const conflicting = merge({ id: 'string-id' }, { id: 42 });
console.log(typeof conflicting.id);  // intersection: string & number = never (TS)
                                      // but at runtime: last value wins

Correct Output

function
function
{"name":"Alice"}
number

Why this output?

Explanation: merged object has all properties from both sources. When the same key conflicts, runtime spread takes the last value (42, a number). TypeScript would type this as never (string & number = never).

Key Insight: Intersection types require ALL properties from all intersected types. When the same property appears with incompatible types, TypeScript resolves it to never — making it unusable. At runtime, object spread takes the last value.

More Union & Intersection Output Questions

MediumUnion type — in operator narrows to the correct variantHardUnion narrowing exhaustion — never in the default case

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz