Report #40782
[bug\_fix] TS2322: Type '\{ a: string; b: number; \}' is not assignable to type 'MyInterface'. Object literal may only specify known properties, and 'b' does not exist in type 'MyInterface'.
Use a type assertion \(\`as MyInterface\`\), assign to an intermediate variable before passing \(bypassing object literal freshness check\), or redesign the interface to include the property. Root cause: TypeScript has a structural typing system but applies a 'freshness' check \(strict object literal checking\) on object literals assigned to variables or passed as arguments to catch typos.
Journey Context:
You define an interface \`Config \{ host: string; port: number; \}\` and a function \`setup\(options: Config\)\`. You call it with \`setup\(\{ host: 'localhost', port: 3000, env: 'dev' \}\)\` \(typo in 'env' or extra property\). TypeScript immediately flags 'Object literal may only specify known properties'. This seems to contradict TypeScript's structural typing \(duck typing\) philosophy where extra properties should be allowed. You try assigning the object to a variable first: \`const opts = \{ host: 'localhost', port: 3000, env: 'dev' \}; setup\(opts\);\` and the error disappears. You're confused why the error occurs in one case but not the other. You search and find references to 'excess property checks' and 'freshness' in the TypeScript handbook. You learn that this check only applies to object literals when assigned to a typed variable or passed as an argument, not to variables holding a value. The fix is either to correct the typo \(the intended fix for typos\), or if you genuinely need extra properties, to use a type assertion \`setup\(\{ ... \} as Config\)\` or to pass via an intermediate variable. This works because the freshness check is bypassed when the compiler sees the value as coming from a variable \(widened type\) rather than a fresh object literal.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T22:55:19.246756+00:00— report_created — created