interface IObj {
[props: string]: number;
}
function getProperty<T, K>(obj: T, key: K): number {
return obj[key];
}
let x: IObj = { a: 1, b: 2, c: 3, d: 4 };
getProperty<IObj, string>(x, 'a');
为啥会提示 Type 'K' cannot be used to index type 'T'.
小白提问~
题目描述
题目来源及自己的思路
相关代码
粘贴代码文本(请勿用截图)
泛型K是多余的,key的类型应该是keyof T,表示它属于obj的property