Describing this concept can be somewhat challenging, so I'll just present the code instead:
type FormFields = {
email: string;
count: number;
};
type FieldMap<T extends Record<string, unknown>> = Record<
keyof T,
FieldMapping<T>
>;
interface FieldMapping<Values extends Record<string, unknown>> {
label: string;
initialValue: Values[keyof Values];
}
const formDetails: FieldMap<FormFields> = {
email: {
label: "Email",
initialValue: "", // should enforce string here
},
count: {
label: "Count",
initialValue: "test", // string should not be allowed here - should enforce number
},
};
Is there a way to ensure that the initialValue
parameter adheres to the typings specified in the FormFields
based on the key? The issue seems to lie with
initialValue: Values[keyof Values]
- but the solution eludes me.