typescripttypescript2.2

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


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'.


Solution

  • 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;
    }