Sub topic 6
Catching Typos and Excess Properties
Assume you know TypeScript object types, union types, type aliases, and basic type inference. This chapter focuses on one practical use of satisfies: finding misspelled or unexpected properties in configuration objects without losing the object's precise inferred type.
What is an excess property?
An excess property is an object property that does not belong to the target type. For example, host belongs to Options, but hots does not:
type Options = {
port: number;
host?: string;
};
const options: Options = {
port: 8080,
hots: "localhost",
};TypeScript reports an error similar to this:
Object literal may only specify known properties, and 'hots' does not exist in type 'Options'.This check is useful because a spelling mistake in a configuration object often causes a runtime bug. The program may silently ignore the misspelled property.
TypeScript performs this check especially when an object literal is assigned directly to a typed variable or passed to a function that expects a specific type.
Why `satisfies` helps
The satisfies operator checks that an expression is compatible with a type. It does not replace the expression's inferred type with that target type.
type Options = {
port: number;
host?: string;
};
const options = {
port: 8080,
hots: "localhost",
} satisfies Options;The typo is still reported:
Object literal may only specify known properties, and 'hots' does not exist in type 'Options'.That is the important behavior. satisfies validates the object's shape, including its property names, while preserving the type inferred for valid properties.
Direct assignment versus an intermediate variable
Excess property checking has a common edge case. TypeScript often checks an object literal directly, but it may allow an extra property after the object has first been stored in an unannotated variable.
type Options = {
port: number;
};
const raw = {
port: 8080,
hots: "localhost",
};
const options: Options = raw; // accepted
console.log(options.port);This assignment is accepted because TypeScript uses structural typing. The source object has the required port property, so it can be used where Options is expected. The extra hots property does not prevent assignment.
The property still exists at runtime, but it is not part of the static type of options.
Use satisfies at the object declaration when the object itself must be checked:
type Options = {
port: number;
};
const options = {
port: 8080,
hots: "localhost",
} satisfies Options;Now TypeScript checks the object literal before it can hide the typo inside an intermediate variable.
Nested objects are checked too
When a type describes nested objects, satisfies provides contextual typing for those nested values. This lets TypeScript find misspelled nested properties.
type ServerConfig = {
server: {
host: string;
port: number;
};
logging: {
enabled: boolean;
};
};
const config = {
server: {
host: "localhost",
port: 3000,
prot: "http",
},
logging: {
enabled: true,
},
} satisfies ServerConfig;TypeScript reports that prot is not a known property of the nested server object. The expected property might have been protocol, but that property is not in ServerConfig either. The type must describe every property that the configuration is allowed to contain.
A valid configuration can still keep useful local precision:
type ServerConfig = {
server: {
host: string;
port: number;
};
logging: {
enabled: boolean;
};
};
const config = {
server: {
host: "localhost",
port: 3000,
},
logging: {
enabled: true,
},
} satisfies ServerConfig;
console.log(config.server.port.toFixed(0));
console.log(config.logging.enabled.valueOf());How the three choices differ
| Choice | Checks compatibility | Preserves the expression's precise type | Rejects direct excess properties |
|---|---|---|---|
| Type annotation | Yes | Usually no | Yes |
| `as` assertion | No reliable verification | Often yes | No |
| `satisfies` | Yes | Yes | Yes |
A type annotation changes the variable's static type to the annotation:
type Options = {
port: number;
host?: string;
};
const annotated: Options = {
port: 8080,
host: "localhost",
};
// Only the properties declared by Options are available.
console.log(annotated.port);An assertion tells TypeScript to trust you:
const asserted = {
port: 8080,
hots: "localhost",
} as Options;
// The typo was not checked.
console.log(asserted.port);Prefer satisfies when the object is a value that must obey a known shape, especially for settings, route maps, feature flags, and other static configuration.
A visual model
Exercise
Exercise
Why does const raw = { port: 8080, hots: "localhost" }; const options: Options = raw; potentially compile, while the same object written as const options = { port: 8080, hots: "localhost" } satisfies Options; produces an error?
Show answer
The first assignment uses structural typing after the object has been stored in a variable, so the extra property can be accepted. The second expression applies an excess property check directly to the object literal, so hots is rejected.