TypeScript is structurally typed, so extra properties are normally fine: an object with more fields is assignable to a type that needs fewer. The exception is excess property checking: when a "fresh" object literal is directly assigned to a typed variable, passed as an argument, or returned from a function with an annotated return type, unknown properties are an error. The goal is catching typos and wrong options ({ colour: 'red' } instead of color). Freshness is lost once the literal is stored in a variable without an annotation, which is why the check sometimes "doesn't fire".
interface ButtonOptions {
label: string;
color?: 'blue' | 'red';
disabled?: boolean;
}
function createButton(opts: ButtonOptions) {}
createButton({ label: 'Save', colour: 'red' });
// error: Object literal may only specify known properties,
// but 'colour' does not exist in type 'ButtonOptions'. Did you mean to write 'color'?
Without this check, the typo would compile silently, because all of ButtonOptions's other properties are optional, and the button would render with the default color.
const opts = { label: 'Save', colour: 'red' };
createButton(opts); // no error: not fresh, structural assignability only
Other cases where it doesn't fire:
{ label: 'x', extra: 1 } as ButtonOptions.{ ...base, label: 'x' }, properties coming from base aren't checked.[key: string]: unknown), which accept any extra keys.fn<T>(x: T)), since T becomes the literal's type.For union targets, a property is "excess" only if it exists on none of the members (and with discriminated unions, TS checks against the matching member):
type Shape = { kind: 'circle'; radius: number } | { kind: 'square'; size: number };
const s: Shape = { kind: 'circle', radius: 1, size: 2 }; // error: 'size' not in the circle variant
A weak type has only optional properties. Assigning any value that shares no properties with it is an error, even if it isn't fresh:
interface Options { timeout?: number; retries?: number }
const config = { timout: 100 };
const o: Options = config; // error: no properties in common
This catches the fully-typo'd case where freshness has already been lost.
const opts: ButtonOptions = { … } gets the check where the object is written.satisfies: const opts = { … } satisfies ButtonOptions checks excess properties and keeps the precise inferred type.& Record<string, unknown>, or an explicit meta?: Record<string, string> field) rather than asserting.Object.keys and serialization may include them. Strip unknown keys with a schema (.strict() or .strip() in Zod) if it matters, for example before sending data to a server.as rather than fixing a typo.satisfies/annotations at creation, and runtime stripping when it matters.Spotted a mistake or a rights concern? Report an issue