I want the following example to produce a type error:
interface Person {
age: number;
name: string;
birthday: Date;
}
interface Grid<T> {
columns: {
field: keyof T;
formatter: (value: T[keyof T]) => string;
}[];
}
function draw<T>(grid: Grid<T>) {}
draw<Person>({
columns: [
{
field: "age",
formatter: (value: number) => "",
},
{
field: "name",
formatter: (value: number) => "", // <-- this parameter should be a `string`! However, TS allows this because `T[keyof T]` matches.
},
],
});
To what should I change the type signature for the formatter function so that its parameter matches the type of the field?