ts重点学习17-数组类型笔记

export default {}

// interface IKeyInterface {
//   [key: string]: any
// }

// let getProps = (obj: IKeyInterface, key: string): any => {
//   return obj[key];
// }

// // {a:1, b: 2}

// let x = {a:1, b: 2};
// // let res = getProps(x, "b");
// let res = getProps(x, "c"); // undefined
// console.log(res);


// 注意点: K 就代表 T 中必须有的属性
// T: number, age   K: number / age  != sex
function getProperty<T, K extends keyof T>(obj: T, key: K) {
  return obj[key];
}

let x = {a: 1, b: 2};
// let res =  getProperty(x, "c");
// console.log(res);

posted @ 2022-09-30 20:42  前端导师歌谣  阅读(21)  评论(0)    收藏  举报