In TypeScript, is it possible to pass each key in an object as a generic to each corresponding value in that object? For instance:
interface Items
{
[key: Key extends string]: Item <Key>;
};
interface Item <Key>
{
name: Key;
};
This can be achieved if the keys are a string literal:
type Name = 'a' | 'b' | 'c';
type Items =
{
[Key in Name]?: Item<Key>;
};
interface Item <Name>
{
name: Name;
};
const items: Items =
{
// Type valid
a:
{
name: 'a'
},
// Type error
b:
{
name: 'c'
}
};
I'm not sure how this can be extended to allow any string. Any help would be much appreciated.