I am currently working with different types of data
enum DataTypes {
Email = 'email',
Checkbox = 'checkbox',
}
type DataTypeValues = {
[DataTypes.Email]: string;
[DataTypes.Checkbox]: boolean;
};
type Type1<T extends DataTypes> = { type: T } & {
[key in T]: DataTypeValues[T];
};
type Type2 = Type1<DataTypes.Email | DataTypes.Checkbox>
In the given example, Type2
looks like this:
{
type: 'email' | 'checkbox',
email: string,
checkbox: boolean
}
However, I actually need it to be structured as follows:
{
type: 'email',
email: string
} | {
type: 'checkbox',
checkbox: boolean
}
I have extensively studied TypeScript documentation on typings and found some interesting information, but nothing that addresses my specific scenario.
Is there a way to achieve the desired structure, or should I consider modifying my base types (DataTypes
, DataTypeValues
)?