The unrelated image shows the water system in our garden. Now, on to index types in TypeScript.
Address
defines an interface. AddressBook
defines an interface with a string index signature. The compiler will check that all of the properties of an AddressBook
are of type Address
. For instance, addressConstants
is an object that can contain only properties of type Address
.
Here it is in the TypeScript playground.
interface Address {
street: string;
postalCode: string;
province: string;
country: string;
}
interface AddressBook {
[key: string]: Address;
}
const addressConstants = {
// allowed
OWNER: {
street: "...",
postalCode: "...",
province: "....",
country: "...",
},
// forbidden
// OWNER_NAME: {
// firstName: "...",
// lastName: "...",
// }
} as AddressBook;