【发布时间】:2021-07-16 12:15:18
【问题描述】:
我希望以下示例产生类型错误:
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.
},
],
});
我应该更改格式化函数的类型签名以使其参数与字段的类型匹配?
【问题讨论】:
-
我相信这个答案可以帮助你stackoverflow.com/questions/65129070/…这里catchts.com/callbacks你可以找到一些额外的信息
标签: typescript