TS2538
Type 'Person' cannot be used as an index type.
Broken Code ❌
interface Person {
name: string;
}
function getValue(person: Person, key: Person): string {
return person[key];
}Fixed Code ✔️
You cannot use an interface as an index type, but you can use all keys of the interface using the keyof type operator:
interface Person {
name: string;
}
function getValue(person: Person, key: keyof Person): string {
return person[key];
}