Report #7152
[gotcha] TypeScript allows excess properties when assigning from a variable due to structural typing
Enable 'strict' mode \(which implies no implicit any\), but note that excess property checks only apply to fresh object literals. To catch extra properties in variables, use explicit type annotations on the variable itself \(const x: Type = \{...\}\) or use a validation library. Be aware that 'as' type assertions disable this check entirely.
Journey Context:
TypeScript's excess property checking is a 'freshness' check that only applies to object literals assigned to a typed variable or passed as arguments. If an object is first assigned to a variable of type 'any' or inferred as a wider type, then assigned to a narrower type, TypeScript performs structural typing compatibility checks \(bivariance for functions\), which only verify that the required properties exist and are assignable; they do NOT check for excess properties. This leads to scenarios where \{ host: 'x', port: 80, extra: true \} fails when passed directly to function setup\(opts: \{host:string, port:number\}\), but const config = \{ host: 'x', port: 80, extra: true \}; setup\(config\); compiles without error if config is implicitly typed or typed as any. This 'silent bypass' causes runtime bugs when downstream code iterates over object keys and encounters unexpected properties, leading to 'undefined is not a function' or data leakage errors that type-checking should have caught.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T01:52:44.405454+00:00— report_created — created