I have a Column interface that can render values from rows based on the keys:
interface Column<Row, Field extends keyof Row> {
key: Field;
render: (value: Row[Field]) => React.ReactNode;
}
If I explicitly set the type of each column, then I get the desired behaviour. For instance, the following is invalid:
interface User {
id: number;
name: string;
}
const column: Column<User, "id"> = {
key: "id",
render(id: string) {
return id;
}
};
since the id property is a string, not a number. Is there a way to get this behaviour without having to specify the type of each column individually? For instance, the following type-checks:
const columns: Array<Column<User, keyof User>> = [
{
key: "id",
render(id: string) {
return id;
}
}
];
since Field is instantiated to keyof User, rather than a specific key.
id: numberas the argument.