Skip to content

Excess property check only on object literals

Quick summary

You assigned a variable with extra properties to a type — TypeScript does not check excess properties from variables, only from literals.

Why this happens

  • An object literal with a typo'd property name is first assigned to a variable, bypassing excess property checks

  • A variable with extra properties is passed to a function, and the extra properties are silently allowed

  • You expected TypeScript to catch extra properties from variables, but it only checks object literals

Minimal example

✗ Broken code
interface Config { host: string; port: number; }
const config = { host: 'localhost', port: 8080, prot: 9000 }; // typo: 'prot'
function connect(c: Config) { /* ... */ }
connect(config); // no error! excess check skipped for variables
✓ Fixed code
interface Config { host: string; port: number; }
function connect(c: Config) { /* ... */ }
connect({ host: 'localhost', port: 8080, prot: 9000 }); // error: 'prot' does not exist in type 'Config'

Passing the object as a literal triggers TypeScript's excess property check, catching the typo'd 'prot' property at compile time.

How to diagnose

  • Check if the object is passed as a literal or a variable — only literals get excess property checks

  • Compare the object properties with the target type to find any extra or typo'd properties manually

  • Use a type assertion or a constructor function to force excess property checking on variables

How to fix

  • Pass the object as a literal directly to the function to trigger excess property checks

  • Use a factory function that takes the exact type and returns it, forcing the caller to match the type

  • Use a schema validation library (zod) to validate the object shape at runtime

How to prevent

  • Pass object literals directly to functions when possible to get excess property checks for free

  • Use factory functions or builder patterns that enforce the exact type at construction time

Related Resources

Related Glossary

Related Lessons

← Back to language