TS2559

error TS2559: Type ‘{ city: string; }‘ has no properties in common with type ‘User‘.

Broken Code ❌

1
2
3
4
5
6
7
8
9
10
11
12
13
14
interface User {
age?: number;
name?: string;
}

function increaseAge(user: User): void {
if (user.age) {
user.age += 1;
}
}

const user = {city: 'Berlin'};

increaseAge(user);

Fixed Code ✔️

The problem occurs with weak types (in this case User). All properties of a weak type are optional but when your input doesn’t match any of the properties, you will get to see error TS2559. To fix it you have to match at least one of the optional properties:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
interface User {
age?: number;
name?: string;
}

function increaseAge(user: User): void {
if (user.age) {
user.age += 1;
}
}

const user = {age: 1, city: 'Berlin'};

increaseAge(user);