Workarounds: Variable Assignment
What you'll learn: How to use an intermediate variable to bypass excess property checks when you genuinely need extra properties.
The Workaround Explained
Remember how TypeScript performs excess property checks when you pass object literals directly to functions or assign them to typed variables? Sometimes you legitimately need those extra properties (perhaps for logging, debugging metadata, or future extensibility), but the direct assignment fails.
The solution is simple: assign the object literal to a variable first.
When you do this, the object becomes "non-fresh" — TypeScript performed the excess property check on the original assignment (which was to an untyped variable, so it passed), and now the variable holding that object is just treated as compatible based on structure, not freshness.
How It Works
interface Config {
timeout: number;
}
function initialize(config: Config) {
console.log(config.timeout);
}
// Direct: ❌ Error - excess property
// initialize({ timeout: 5000, debug: true });
// Workaround: ✅ Works
const myConfig = { timeout: 5000, debug: true };
initialize(myConfig);
By storing the object in myConfig first, you've broken the "freshness." TypeScript now just checks: "Does myConfig have a timeout that's a number?" Yes. The debug property is ignored because the object isn't fresh anymore.
When to Use This
This workaround is legitimate when you know the extra properties won't cause problems — perhaps you control both the object and the function, or you're working with a flexible API that ignores unknown properties.
Key Takeaway: Assigning an object literal to an intermediate variable makes it "non-fresh," bypassing excess property checks while maintaining structural compatibility.