面试题答案
一键面试// 定义映射结构的类型
type ComplexMap<T> = {
[key: string]: T[];
};
// 泛型函数,根据键获取对应数组中的特定索引位置的值
function getValueByIndex<K extends string, T>(map: ComplexMap<T>, key: K, index: number): T | undefined {
if (map.hasOwnProperty(key)) {
const array = map[key];
if (index >= 0 && index < array.length) {
return array[index];
}
}
return undefined;
}