I'm trying to type an interface (or type) with the following logic:
interface MyArgs<Name, Id> {
items: {
[Name]: string,
[Id]: string
}[];
idKey: string;
nameKey: string;
}
In more detail:
I'm getting an array of items of an unknown type/interface (it's dynamic, they can be anything). While I could just type them as any[], I'd like to know if it's possible to be a bit more specific about their type, and pass Typescript the information that I do have:
each item will be having an id property (e.g: guid, id, uuid) and a name property (e.g: name, label, title). The exact idKey and nameKey are provided as arguments (idKey, nameKey).
I want to declare the values provided in idKey and nameKey as valid keys for items records (e.g: { [idKey]: string; [nameKey]: string }(
What I tried above doesn't work. I also tried this:
interface MyArgs<Name, Id> {
items: { <Name>: string; <Id> : string }[]
idKey: string
nameKey: string
}
which is, too, invalid.
How can I achieve this?
Note: if the above would be possible without having to type the values twice (once inside <> and once in the values of idKey and nameKey, that would be even more awesome).
item[idKey]is astringand so isitem[nameKey], without me having to declareas string.inferis the right way to do it, but the implementation details for my specific case elude me (I keep getting errors). I'd appreciate an answer if you happen to know how to do it. Thanks!