Reputation: 637
There is a set of interfaces:
export interface FormData<T extends ControlData = any> {
[type: string]: T;
}
export type FormResult<T extends FormData> = {
[type in keyof T]: T[type];
};
export interface ControlData<T = any> {
value: T;
}
export interface ButtonSelectControlData<T> extends ControlData<T> {
query: string;
}
export interface RoutesAddCityData extends FormData {
cityId: ButtonSelectControlData<number>;
routeId: ControlData<number>;
}
When I use FormResult:
(data: FormResult<RoutesAddCityData>) => {
// ...
}
As expected I see in IDE that data.cityId
has type ButtonSelectControlData<number>
and data.routeId
has type ControlData<number>
.
But I would like to see type number in both cases. Is it possible?
Upvotes: 0
Views: 40
Reputation: 25820
If I understand correctly, you want the FormResult
type to extract the underlying type parameters from ButtonSelectControlData
and ControlData
. If that's the case, then one possible solution is to create a conditional type:
type FormResult<T> = {
[K in keyof T]: T[K] extends ControlData<infer U>
? U
: never
}
Upvotes: 1