Structural Typing and Excess Property Checks in TypeScript
TypeScript compares shapes, not names. Why an object literal gets an "Object literal may only specify known properties" error and a variable does not.
In this chapter
Structural typing and excess property checks
More on Everyday types
- Why TypeScript? Benefits Over JavaScript, Explained
- TypeScript Types: Primitives, Arrays and Tuples
- TypeScript Function Types: Parameters, Overloads, void
- TypeScript Interface vs Type Alias, and Object Types
- any vs unknown vs never in TypeScript
- TypeScript readonly, as const and Type Widening
- TypeScript Classes: private, #private, abstract, implements
- TypeScript Enums vs Union Types: Should You Use Enums?
- Annotate or let TypeScript infer
Keep learning
Overview
chapters · everyday types Structural typing and excess property checks TypeScript compares shapes, not names. A fresh object literal gets one extra check.
1. The problem: the same typo, two outcomes
Both sides pass options to a toast pop-up. Both misspell duration as duraton. (? after a key means "may be left out".) via a variable: no error No error. toast never reads duraton, so the pop-up closes after its default time, not 5000 ms. inline literal: caught Object literal may only specify known properties, but 'duraton' does not exist in type 'ToastOptions'. Did you mean to write 'duration'? The object is written right inside the call, so TypeScript checks for unknown keys and finds duraton. Two rules explain this: structural typing, and excess property checks on fresh literals.
Why the left side passes
opts is a variable, not an object written in the call, so no unknown-key check runs toast('Order placed', opts) TypeScript only asks: do the keys ToastOptions knows have the right types? duration is optional, so its absence is fine extra keys are allowed on a variable
2. Shape, not name
any object has every required key? with a compatible type assignable (it fits) the name of its class is never checked Structural typing (compatibility by shape): if it has the right properties, it fits. In the code, constructor(public amount: number) is a shortcut: it creates an amount property on each object.
Two classes, one shape
assignable to Dollars? new Dollars(5) new Euros(5) same shape plain objects work too amount has the wrong type amount is missing
3. Object types are open, not sealed
What you picture Point means exactly x and y. What TypeScript means Point means at least x and y. More keys are fine.
Why Object.keys gives string[]
key: string Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Point'. At runtime p3 also has z. So Object.keys cannot promise only "x" | "y" (the union "x or y"). It returns string[]. The error on line 6, in plain words: key could be any string, such as "z". Point has no type for p["z"], so TypeScript could not work out a type and gave up (that is "implicitly any"). Strict mode turns that into an error.
4. Excess property checks: only for fresh literals
A fresh literal (an object written right where the type is expected) has no other users. Extra keys there are almost always typos. Terms in the table: ...home (spread) copies all keys of home. as Address says "trust me, this is an Address". satisfies Address checks the value against Address but keeps its own exact type. where the object comes from extra key flagged? fresh literal fresh literal as argument a spread literal is still fresh satisfies checks the literal a variable is not fresh as switches the check off Is line 4 an error? no error error: 'z' does not exist in type 'Point' error: missing property z
5. Weak types: all keys optional
A weak type (every property optional) would accept almost anything. So TypeScript adds one rule: share at least one key. no key in common ToastOptions has only optional keys. opts shares none of them (duraton ≠ duration), so it is rejected. This rule works even for variables. one key in common: passes The typo slips through again: dismissible matches, so the check is satisfied.
6. Functions with fewer parameters fit
needs more than the caller gives ignores some of what it gets forEach passes three arguments. Your callback reads one. Fewer params: safe JavaScript drops extra arguments. Nothing reads them. More params: unsafe selected would be undefined at runtime.
7. When names should matter
A private (or #private) field can be used only inside its own class. It makes classes stop matching by shape. Type 'OrderId' is not assignable to type 'UserId'. Types have separate declarations of a private property 'brand'. Why: the two shapes look the same, but each private brand belongs to its own class. TypeScript treats two private fields from different classes as different keys. So an OrderId does not fit into UserId. Euros as Dollars OrderId as UserId Plain classes / types Class with a private field Branded type
Remember
Compatibility is decided by shape, never by type name. Object types mean “at least these keys”, so extra keys are legal. All-optional types must share at least one key with the value. ! Cheat code Shape decides. Write options objects inline so typos get caught. Need names to matter? Add a private field or a brand. → Go deeper: object types and interfaces, satisfies and one source of truth, branded types, typing functions.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.