Dynamic Default Properties
What
Dynamic default properties allow you to require specific keys on an object while still permitting any number of additional dynamic keys.
How
You list the required properties alongside the index signature. Note that all properties must conform to the value type defined in the index signature:
type FormData = {
[field: string]: string | number;
email: string; // Required
password: string; // Required
age: number; // Required
};
Why
This pattern is useful for data structures that must have a minimum set of “standard” fields but can be extended with custom user data. It provides the strictness of a predefined schema with the flexibility of a dictionary.
Summary
Use this pattern sparingly; if you have a known set of optional fields, preferred using the ? operator instead.