TypeScript Master (50 Qs)•Part 2: Advanced Type System & Metaprogramming (Mid-Level)
What are Excess Property Checks?
junior LevelCode ExampletypescriptAdvanced Type System
In Simple Words: TypeScript normally allows passing an object with extra properties where fewer properties are expected (structural typing). However, when passing an object literal directly, TypeScript triggers Excess Property Checks to prevent typos:
typescript
interface Options { title: string; }
// Error: 'colour' does not exist in type 'Options' (Did you mean 'color'?)
const opt: Options = { title: "App", colour: "red" };
// Allowed when assigned to an intermediate variable first:
const raw = { title: "App", colour: "red" };
const opt2: Options = raw; // Compiles!