面试题答案
一键面试class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
function getPropertyValue<T, K extends keyof T>(obj: T, propertyName: K): T[K] {
return obj[propertyName];
}
// 使用示例
const person = new Person('Alice', 30);
const nameValue = getPropertyValue(person, 'name');
const ageValue = getPropertyValue(person, 'age');
console.log(nameValue);
console.log(ageValue);