You can use mapped types for this purpose
interface StateVariable<T> {
value: T;
setValue: (value: T) => void;
}
interface ImportAccountsState {
isDialogOpen: StateVariable<boolean>;
organization: StateVariable<string>;
isOrganizationValid: StateVariable<boolean>;
}
type Result = {
[Prop in keyof ImportAccountsState]: ImportAccountsState[Prop]['value']
}
Docs
Would it be far more complex if ImportAccountsState was generic?
interface StateVariable<T> {
value: T;
setValue: (value: T) => void;
}
// not sure where you want to use T generic
interface ImportAccountsState {
isDialogOpen: StateVariable<boolean>;
organization: StateVariable<string>;
isOrganizationValid: StateVariable<boolean>;
}
type Result<T extends Record<PropertyKey, { value: unknown }>> = {
[Prop in keyof T]: T[Prop]['value']
}
I am getting an error when using Result
Just use type instead of interface:
// not sure where you want to use T generic
type ImportAccountsState = {
isDialogOpen: StateVariable<boolean>;
organization: StateVariable<string>;
isOrganizationValid: StateVariable<boolean>;
}
types have index signature if you want to constraint it
OR move constraint inside the iteration:
interface StateVariable<T> {
value: T;
setValue: (value: T) => void;
}
// not sure where you want to use T generic
interface ImportAccountsState {
isDialogOpen: StateVariable<boolean>;
organization: StateVariable<string>;
isOrganizationValid: StateVariable<boolean>;
}
type Util<T> = {
[Prop in keyof T]: T[Prop] extends { value: unknown } ? T[Prop]['value'] : never
}
type Result = Util<ImportAccountsState> // ok