TS2344

error TS2344: Type ‘{ name: string; }‘ does not satisfy the constraint ‘{ age: number; }‘. Property ‘age‘ is missing in type ‘{ name: string; }‘ but required in type ‘{ age: number; }‘.

Broken Code ❌

1
2
3
4
5
6
function increaseAge<T extends { age: number }>(data: T): T {
data.age += 1;
return data;
}

increaseAge<{ name: string }>({age: 25, name: 'Benny'});

Fixed Code ✔️

When passing a type argument, the required properties of the type variable (T) must be matched:

1
2
3
4
5
6
function increaseAge<T extends { age: number }>(data: T): T {
data.age += 1;
return data;
}

increaseAge<{ age: number, name: string }>({age: 25, name: 'Benny'});

As an alternative, type argument inference can be used:

1
2
3
4
5
6
function increaseAge<T extends { age: number }>(data: T): T {
data.age += 1;
return data;
}

increaseAge({age: 25, name: 'Benny'});