Freshness and Excess Property Checking
Explore why excess properties trigger errors in some contexts.
Freshness and Excess Property Checking is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 propertyWhy Excess Property Checking Exists
It catches typos and unintended properties in configuration objects, a common source of bugs in JavaScript.
interface Options { timeout: number; retry?: boolean; }
const opts: Options = { timeout: 3000, retrey: true }; // Error — typo!How Freshness Is Lost
Once an object literal is assigned to an intermediate variable, it is no longer "fresh" and excess property checking does not apply to the widened type.
const opts = { timeout: 3000, retry: true, extra: "surprise" };
const o: Options = opts; // No error — freshness lost on assignmentType Assertions Bypass Freshness
Using as type assertions also bypasses excess property checks, which can hide bugs.
const o = { timeout: 3000, typo: true } as Options; // No errorExcess Checking with Intersection Types
Intersection types can sometimes relax excess property checks because the extra properties may belong to one of the intersection members.
type A = { x: number };
type B = { y: number };
const ab: A & B = { x: 1, y: 2 }; // OKReadonly and Excess Properties
Excess property checking applies to readonly object types too — the rule is about structural shape, not mutability.
interface Cfg { readonly host: string; }
const c: Cfg = { host: "localhost", port: 3000 }; // ErrorIndex Signatures and Excess Properties
Adding an index signature to a type allows any additional string-keyed properties, effectively disabling excess property checking for that type.
interface Flexible { name: string; [key: string]: unknown; }
const f: Flexible = { name: "ts", extra: true }; // OKOptional Properties and Freshness
Optional properties do not relax excess property checking. A property that is not listed at all — even as optional — is still reported as excess.
interface Shape { color?: string; }
const s: Shape = { color: "red", size: 10 }; // Error: 'size' excessPractical Patterns
To pass extra config properties through a typed interface without errors, use an intermediate variable or an index signature in the interface.
// Option 1: intermediate variable (freshness lost)
const cfg = { timeout: 3000, debugMode: true };
const o: Options = cfg;
// Option 2: index signature in interfaceRecap: Freshness
Freshness applies to fresh object literals, enabling excess property checking at the point of assignment. Assign to an intermediate variable or use an index signature to allow extra props.
Quick Check
When does excess property checking NOT apply?
What You Learned
Freshness and excess property checking protect you from typos and unintended properties in object literals. Understand when freshness is lost to avoid surprising type errors or hidden bugs.
Frequently asked questions
Is the “Freshness and Excess Property Checking” lesson free?
Yes — the full text of “Freshness and Excess Property Checking” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Freshness and Excess Property Checking”?
Explore why excess properties trigger errors in some contexts. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Freshness and Excess Property Checking” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Type Widening and Narrowing Mechanics
- Contextual Typing: Inference from Context
- Freshness and Excess Property Checking
- const Assertions and as const