Freshness and Excess Property Checking
Explore why excess properties trigger errors in some contexts.
What Is Freshness?
Freshness is a TypeScript concept where object literals are considered "fresh" at the point of creation. Fresh objects undergo strict excess property checking that widened objects do not.
interface Point { x: number; y: number; }
const p: Point = { x: 1, y: 2, z: 3 }; // Error: 'z' not in PointExcess Property Checking in Action
When you assign an object literal directly to a typed variable or pass it as a function argument, TypeScript reports extra properties that are not in the target type.
function setPoint(p: { x: number; y: number }) {}
setPoint({ x: 1, y: 2, z: 3 }); // Error: 'z' excess propertyAll lessons in this course
- Type Widening and Narrowing Mechanics
- Contextual Typing: Inference from Context
- Freshness and Excess Property Checking
- const Assertions and as const