Course contentsShow
← TypeScript
Lesson 331 of 1,7967. ObjectsFree lesson

Property Override Order and Type Merging

Understanding how later spreads override earlier properties and how TypeScript merges the resulting types.

Property Override Order and Type Merging

You'll learn: How TypeScript handles types when object spreads overwrite properties in later positions.

The Override Rule

When you spread multiple objects together in JavaScript, properties from later spreads overwrite properties from earlier ones. TypeScript's type system mirrors this behavior exactly:

const first = { name: "Alice", age: 30 };
const second = { age: 25, city: "NYC" };

const merged = { ...first, ...second };
// Result: { name: "Alice", age: 25, city: "NYC" }
//         age from 'second' overwrites age from 'first'

How TypeScript Merges the Types

TypeScript doesn't just pick the last type — it merges all properties and uses the type from the rightmost spread for any duplicates:

const base = { id: 42, active: true };
const update = { active: false };

const result = { ...base, ...update };
// Type: { id: number; active: boolean }
// Value at runtime: { id: 42, active: false }

The type of result.active is still boolean (not narrowed to false), but TypeScript knows the property exists and tracks the correct type.

Different Types, Same Property

When spreads have the same property name with different types, the rightmost type wins:

const obj1 = { value: "hello" };
const obj2 = { value: 100 };

const combined = { ...obj1, ...obj2 };
// Type: { value: number }
// The string type from obj1 is completely replaced

Think of it like: spreading objects is like painting layers on a canvas — later layers completely cover earlier ones, both in value and type.

Key Takeaway: TypeScript merges spread object types by combining all properties, with rightmost spreads winning type conflicts — matching JavaScript's runtime override behavior.