Index Signatures to Allow Extra Properties
What you'll learn: How to intentionally permit additional properties on objects using index signatures, bypassing excess property checks by design.
Why You'd Need This
Sometimes you genuinely want an object to accept extra properties—not as a workaround, but as part of your design. Rather than fighting excess property checks with type assertions or intermediate variables, you can use an index signature on the target type to explicitly allow additional properties.
The Solution: Index Signatures as Permission
When a type includes an index signature, TypeScript understands that extra properties are expected. This turns off excess property checks for that type, because you've declared "this type can have any number of additional properties."
type Config = {
name: string;
[key: string]: unknown; // Allows any extra properties
};
const config: Config = {
name: "App",
version: "1.0", // ✅ Allowed
author: "Dev", // ✅ Allowed
};
The index signature [key: string]: unknown says "any string key is valid, and its value can be anything."
Practical Use Case
This pattern is common for configuration objects, API response wrappers, or plugin systems where you know certain properties must exist but want to allow extensions:
type User = {
id: number;
name: string;
[metadata: string]: unknown; // Future-proof for extra fields
};
const user: User = {
id: 1,
name: "Alice",
theme: "dark", // ✅ Fine
lastLogin: "2024", // ✅ Fine
};
Important Constraint
Remember the index signature constraint rule: known properties must be compatible with the index signature type. If you use [key: string]: unknown, that means id and name must also be assignable to unknown (which they are, since unknown accepts everything).
Key Takeaway: Use index signatures on your target types when you intentionally want to allow extra properties—it's a design choice, not a hack, and clearly signals your intent to future readers.