Index signatures
Index signatures are used to express the shape of key/value pairs.
typescript
type Fruit = "apple" | "banana" | "orange";
type Prices = {
[index: Fruit]: number;
};
// This is equivalent to
type Prices = Record<Fruit, number>;
See index signatures in the TypeScript documentation
So to make a mapping between Fruit
s and Price
s, it looks like:
typescript
type FruitPriceMapping = {
[key: Fruit]: Price;
};
const MAPPING: FruitPriceMapping = {
apple: { value: 25, currency: "aud" },
/* and others go here */
};
Alternatively, TypeScript has the Record
utility type, which you could consider more terse:
typescript
type FruitPriceMapping = Record<Fruit, Price>;