Fresh vs Non-Fresh Objects
You'll learn why TypeScript treats object literals differently depending on where they appear — and when that "extra strict" checking disappears.
What Makes an Object Fresh?
In TypeScript, an object literal is considered fresh when it's created directly at the point where it's being assigned or passed. Think of "fresh" like food straight from the farm — it hasn't been stored anywhere yet.
When an object is fresh, TypeScript applies excess property checks. This catches typos and accidental extra properties.
But once that object is assigned to a variable or passed through an intermediate step, it becomes non-fresh — like produce that's been sitting on your counter. TypeScript relaxes the excess property checking at that point.
The Fresh Example
interface Person {
name: string;
age: number;
}
// ❌ Fresh object literal — excess property check applies
const person1: Person = {
name: "Alice",
age: 30,
occupation: "Engineer" // Error: Object literal may only specify known properties
};
The Non-Fresh Example
// Object assigned to variable first
const tempObj = {
name: "Bob",
age: 25,
occupation: "Designer"
};
// ✅ Non-fresh object — excess property check doesn't apply
const person2: Person = tempObj; // No error!
The tempObj variable holds a non-fresh object. When assigned to person2, TypeScript only checks that name and age exist with correct types. The extra occupation property is ignored.
Why This Distinction Exists
Fresh objects represent your immediate intent — what you're typing right now. Extra properties are likely mistakes. Non-fresh objects may come from external sources or shared code where extra properties are expected and harmless.
Key Takeaway: Object literals are "fresh" when written inline, triggering strict excess property checks. Once assigned to a variable, they become "non-fresh," and those checks no longer apply.