I have a problem with more advanced Generic types in TypeScript.
What I want to do is an array of data where the data itself controlled by the type like:
[
{
type: "T1",
content: number,
},
{
type: "T2",
values: {
id: number,
...
}
}
]
I kinda get stacked after describing the available types like:
enum DataType {
T1 = "t1",
T2 = "t2"
}
So the result must look kinda like this, I guess:
interface DataGeneric<T> {
[T extends DataType.T1]: { content: number },
[T extends DataType.T2]: { values: { id: number, ... } },
} ???
interface Data<T extends DataType = any(?)> extends DataGeneric<T>{
type: DataType,
// and all the other fields are generic from T = DataType
}
const someData: Data[] | undefined = fetchData();
// suggesting that the `type` of element[1] is "t2"
// and VSCode suggestions works correctly too,
// consider that there is an object-field `values` with `id` key
someData[1].values.id
Thanks in advance!