Let's say I have a type that maps to a database table where the keys are the column names:
interface MyTable {
foo: string;
bar: number;
}
And then I have another type that maps from a form field to the table row.
interface MyTableMap<T> {
columnName: keyof T;
formFieldName: string;
value: any;
}
// I want this to generate an error because 12345 is not a string based on the column name of "foo".
const first: MyTableMap<MyTable> = { columnName: "foo", value: 12345 };
In the example above I don't want to define value: any
but rather have it be the type that it is supposed to be based on the value of columnName
. I can't seem to figure out how to do this without passing a second generic. We know the key is "foo" so how can I tell TypeScript what the correct type for value should be?