Sub topic 6
Catching Typos and Excess Properties
This chapter covers one use of satisfies: finding misspelled and unexpected property names in object literals. It assumes you know interfaces, type aliases, optional properties, union types, and the difference between a type annotation and an as assertion. The earlier chapters in this tutorial cover the basic behavior of satisfies.
The kind of bug we are after
Misspelled property names cause some of the quietest bugs in TypeScript. Suppose a function accepts an options object with an optional host property. You write hots by mistake. Nothing crashes. The function sees no host, uses its default, and your code runs against the wrong server.
type Options = { port: number; host?: string };
function connect(options: Options): string {
return `${options.host ?? "0.0.0.0"}:${options.port}`;
}
console.log(connect({ port: 80, host: "localhost" })); // localhost:80If you pass { port: 80, hots: "localhost" } directly to connect, the compiler rejects it. The rest of this chapter explains why it rejects that call, why the same check can disappear in other positions, and how satisfies brings the check back without costing you type precision.
Excess property checking
TypeScript is structurally typed. A value is assignable to a type if it has at least the properties the type requires. Extra properties normally do not matter, because a value with more properties is still a valid instance of the narrower type.
There is one exception. When you write an object literal directly in a position that has a target type, TypeScript treats the literal as fresh. A fresh literal is checked for excess properties, and any property that the target type does not declare is an error. The reasoning is that if you wrote out a literal by hand, an unknown property is much more likely to be a typo than intentional extra data.
type Options = { port: number; host?: string };
// Error: Object literal may only specify known properties,
// and 'hots' does not exist in type 'Options'.
const direct: Options = { port: 80, hots: "localhost" };The check depends on freshness. Once the literal is stored in a variable, it is no longer fresh, and the check does not run when you use the variable later.
type Options = { port: number; host?: string };
const raw = { port: 80, hots: "localhost" };
const fromVariable: Options = raw; // compiles: 'raw' is not a fresh literal
console.log(fromVariable.port);Keep this rule in mind. Most of the surprises in this chapter come from it.
Why annotations alone are not enough
An annotation does run the excess property check, as the direct example shows. The problem with annotations is that they widen the type, which an earlier chapter covered. After const config: Options = ..., TypeScript only knows what Options says about the variable.
An as assertion keeps no such guard. An assertion tells the compiler to treat the expression as the target type. The compiler only requires that the two types be sufficiently related, so it accepts a literal with a misspelled property.
type Options = { port: number; host?: string };
// Error: Object literal may only specify known properties, and 'hots' does not exist in type 'Options'.
const checked = { port: 80, hots: "localhost" } satisfies Options;
// An assertion compiles without complaint, so the typo goes unnoticed.
const asserted = { port: 80, hots: "localhost" } as Options;The asserted line compiles because { port: number; hots: string } is assignable to Options. The assertion has nothing to object to. Also, asserted.host is undefined at runtime even though the assertion implies a valid Options.
The three tools compare as follows when applied to a fresh object literal:
| Behavior | Annotation (`: T`) | Assertion (`as T`) | `satisfies T` |
|---|---|---|---|
| Reports unknown (excess) properties | Yes | No | Yes |
| Reports missing required properties | Yes | Not always (`{} as Options` compiles) | Yes |
| Resulting type of the variable | `T` | `T` | The literal's own inferred type |
satisfies is the only one of the three that both checks the literal strictly and leaves the inferred type intact.
How satisfies applies the check
When the compiler meets expression satisfies T, it does two things:
- It checks the expression against
T. Because a literal is fresh at this point, the full excess property check runs, and missing required properties are reported too. - It gives the whole expression its own inferred type, not
T. The target type serves as context for inference, and nothing more.
The following diagram shows the flow for an object literal.
Here is a configuration example where both effects matter. The code compiles and runs:
type Options = { port: number; host?: string };
const options = { port: 80, host: "localhost" } satisfies Options;
// 'port' is number and 'host' is string (not string | undefined),
// because the inferred type of the literal is preserved.
console.log(options.host.toUpperCase()); // LOCALHOST
console.log(options.port.toFixed(1)); // 80.0With an Options annotation, options.host would be string | undefined, and the toUpperCase call would need a check first. With satisfies, TypeScript knows the property is present because the literal says so.
Optional properties are where typos hide
Typos in required properties are usually caught anyway: a misspelled port leaves port missing, and the missing-property error fires. Optional properties are different, because a missing optional property is legal. Without the excess property check, a misspelled optional property would pass silently.
This is why the check matters most for configuration objects with many optional fields. The more optional fields a type has, the more you rely on excess property checking.
type Logging = {
level: "debug" | "info" | "warn" | "error";
file?: string;
colors?: boolean;
maxFiles?: number;
};
const logging = {
level: "info",
file: "app.log",
colors: true,
} satisfies Logging;
console.log(logging.level, logging.file.length);
// Error: 'maxFile' does not exist in type 'Logging'. Did you mean 'maxFiles'?
const typo = { level: "warn", maxFile: 5 } satisfies Logging;The "info" string stays a valid level because the target type provides context, and logging.file.length compiles because file is known to be a string.
Missing keys and misspelled keys together
The same check cooperates with the key checks you get from Record<Color, ...>. If the target requires every member of a union as a key, a misspelled key causes two problems at once. The correct key is missing, and the misspelled one is unknown.
type Color = "red" | "green" | "blue";
type RGB = [number, number, number];
// Error: Property 'blue' is missing, and 'bleu' is not a known property.
const broken = {
red: [255, 0, 0],
green: "#00ff00",
bleu: [0, 0, 255],
} satisfies Record<Color, string | RGB>;The compiler may show only one of the two messages at first, depending on its version. Fixing the typo resolves both. The next chapter covers Record in more detail.
Nested literals are checked too
Freshness applies to every object literal in the expression, not only the outermost one. Typos in nested objects are caught as long as the nested value is written inline.
type ServerConfig = {
port: number;
host?: string;
tls?: { cert: string; key: string };
};
const server = {
port: 8443,
tls: { cert: "server.pem", key: "server.key" },
} satisfies ServerConfig;
console.log(server.tls.cert.endsWith(".pem")); // true
// Error: 'ky' does not exist in type '{ cert: string; key: string; }'
// (the required property 'key' is also missing).
const brokenServer = {
port: 8443,
tls: { cert: "server.pem", ky: "server.key" },
} satisfies ServerConfig;Notice that server.tls.cert is accessible without a check for undefined. The inferred type of server contains a tls property, because the literal has one.
When the check does not run
Because the check depends on freshness, you need to know the cases where it is skipped. In each case satisfies still verifies assignability, so wrong value types and missing required properties are still reported.
The value is not a fresh literal
If you put a non-literal on the left of satisfies, no excess property check applies. This includes variables, function results, and values that come from elsewhere.
type Options = { port: number; host?: string };
const raw = { port: 80, hots: "localhost" };
const viaVariable = raw satisfies Options; // compiles: 'raw' is not fresh
console.log(viaVariable.hots); // the extra property is still thereSo the habit to build is to write satisfies directly on the literal, not on a variable that holds it.
Spread properties are not checked
Properties that arrive through a spread are not examined for excess. Only the properties you write out by name in the literal are.
type Options = { port: number; host?: string };
const base = { port: 80, hots: "localhost" };
const merged = { ...base } satisfies Options; // compiles
const named = { ...base, hots: "localhost" } satisfies Options; // named property compiles too?Do not rely on the last line. Treat spread as a gap in the check, and review the shape of whatever you spread. The first form is the one to remember: a typo inside base passes silently.
The target accepts arbitrary keys
If the target type has an index signature, such as Record<string, number> or { [key: string]: string }, every key name is acceptable, so there are no excess properties by definition. A typo in a key name is valid, since all names are valid.
const scores = { alice: 10, bob: 7, carol: "9" } satisfies Record<string, number>;The satisfies operator still catches carol: "9" as a wrong value type, but it cannot know whether carol is a real name. Use a union of literal keys as the Record key type if you want the keys checked.
Union targets allow properties from any member
For a union that is not discriminated, the check accepts a property if any member of the union declares it. The literal below is accepted even though no single member has both properties.
type Target = { a: number } | { b: string };
const both = { a: 1, b: "x" } satisfies Target; // compiles
console.log(both.a, both.b);A discriminated union is stricter. When a literal property such as kind: "circle" identifies the member, TypeScript checks the remaining properties against that member alone.
A checklist for configuration objects
Use these practices to get the full benefit:
- Write the literal inline and put
satisfiesright after it, with no intermediate variable. - Prefer string-literal unions to
stringfor key types, so the key check has something to compare against. - Be careful with spreads. If you merge objects, add
satisfiesto the pieces you wrote by hand, and keep defaults in a typed constant of their own. - Do not use
asto quiet an error you do not understand. Ifsatisfiesreports an error, the literal and the type disagree, and one of them is wrong. - Remember the check is compile-time only. Data parsed from JSON or the network never goes through it.
Putting it together
This final example combines the checks from the chapter in a small route-handler table. It compiles and runs.
type Method = "GET" | "POST";
type Route = {
method: Method;
path: string;
description?: string;
auth?: boolean;
};
const routes = {
listUsers: { method: "GET", path: "/users" },
createUser: { method: "POST", path: "/users", auth: true },
} satisfies Record<string, Route>;
for (const [name, route] of Object.entries(routes)) {
console.log(name, route.method, route.path);
}
// Each route keeps its own literal types:
const method: "POST" = routes.createUser.method;
console.log(method, routes.createUser.auth);If you wrote athu: true inside one of the entries, the nested excess check would reject it. The Record<string, Route> target does not limit the route names, but it fully checks every route's shape. And because satisfies keeps the inferred type, routes.createUser.method is "POST", not Method.
Summary
- Excess property checking applies to fresh object literals. It treats unknown properties as likely typos.
satisfiesruns this check, as an annotation does, and also keeps the precise inferred type of the expression.- An
asassertion skips the excess check, so it is the wrong tool for catching typos. - The check is most valuable for optional properties, where a missing property would otherwise be legal.
- The check is skipped for non-fresh values, spread members, index-signature targets, and non-discriminated unions.
Exercise
The following code compiles without any error, even though hots is not a property of Options. Why, and what is the smallest change that makes the compiler report the typo?
type Options = { port: number; host?: string };
const raw = { port: 80, hots: "localhost" };
const options = raw satisfies Options;
Show answer
raw is stored in a variable, so it is no longer a fresh object literal, and excess property checking only applies to fresh literals. The smallest fix is to write the literal directly in the satisfies expression: const options = { port: 80, hots: "localhost" } satisfies Options;. The compiler then reports that hots does not exist in Options.
Exercise
Given type Color = "red" | "green" | "blue";, which of the two targets catches the misspelled key in { red: 1, green: 2, bleu: 3 }: satisfies Record<string, number> or satisfies Record<Color, number>? Explain what each does.
Show answer
Record<Color, number> catches it. It requires the keys red, green and blue, so blue is reported as missing and bleu as an unknown property. Record<string, number> has an index signature that accepts any key name, so bleu is valid and nothing is reported. It would still catch a wrong value type, such as bleu: "3".
Exercise
Why is an excess property check more important for optional properties than for required ones? Name one case where { ...defaults, extra: 1 } satisfies Type and { extra: 1 } satisfies Type behave differently, and explain the difference.
Show answer
A misspelled required property also leaves the correct property missing, so the missing-property error exposes it anyway. A misspelled optional property leaves nothing missing, so only the excess property check can catch it. Properties that arrive through a spread are not checked for excess, but properties written by name in the literal are. So if defaults contains a property that Type does not declare, the spread version accepts it silently. The second version, which names extra directly, reports extra as unknown if Type does not declare it.