TS2411

error TS2411: Property ‘age’ of type ‘number’ is not assignable to ‘string’ index type ‘string’.

Broken Code ❌

1
2
3
4
5
interface Person {
[key: string]: string;
age: number;
name: string;
}

Fixed Code ✔️

We defined an interface where every key has a value of type string. This doesn’t work for age which is why we have to extend the possible value types using a union type:

1
2
3
4
5
interface Person {
[key: string]: number | string;
age: number;
name: string;
}