Is it possible to interpret data differently based on a 'type-field'?
I am loading data (all from the same file). I know the type of the data, and what the type definitions look like. With the current union approach, it'd always show all fields, but I'd like to be able to automatically be able to determine which type applies. I could cast the type during runtime, but that's not ideal.
Not sure if I am pushing TS a bit too much here, and if it instead should live in the actual loading/parsing logic?
Current approach
enum MyTypes {
TypeA = "A",
TypeB = "B",
}
type IData = {
type: MyTypes;
data: IDataAllTypes <---- force the type to be `IDataTypeA` if the type field is `TypeA`
}
type IDataAllTypes = IDataTypeA | IDataTypeB
type IDataTypeA = {
id: string
age: number
foo: string[]
}
type IDataTypeB = {
id: string
name: string
bar: string[]
}