error TS2345: Argument of type 'T' is not assignable to parameter of type 'object'

Nenad picture Nenad · Feb 23, 2017 · Viewed 85.5k times · Source

The code below was working fine with TypeScript 2.1.6:

function create<T>(prototype: T, pojo: Object): T {
    // ...
    return Object.create(prototype, descriptors) as T;
}

After updating to TypeScript 2.2.1, I am getting the following error:

error TS2345: Argument of type 'T' is not assignable to parameter of type 'object'.

Answer

Nenad picture Nenad · Feb 23, 2017

Change signature of the function, so that generic type T extends type object, introduced in Typescript 2.2. Use this syntax - <T extends object>:

function create<T extends object>(prototype: T, pojo: Object): T {
    ...
    return Object.create(prototype, descriptors) as T;
}