'The 'this' context of type '...' is not assignable to method's 'this' of type '...'

I'm creating the type definitions (.d.ts) for a JavaScript library. One of the methods of this library recieves a object of functions, then internally uses the Function.prototype.bind in each one of them and expose the modified functions in another context. The new this have it's type well defined, but when I try to call the modified functions, I get the following error:

Both the error messages and the code below are from a simpler reproduction of the original error.

index.ts:37:5 - error TS2684: The 'this' context of type 'Callbacks' is not assignable to method's 'this' of type 'This'.
  Type 'Callbacks' is missing the following properties from type 'This': name, context, number
type Fn = (this: This, ...args: any[]) => any | Promise<any>;

interface This {
    name: string;
    context: any;
    number: number;
}

interface Callbacks {
    [key: string]: Fn;
}

const callbacks: Callbacks = {
    multipleNames() {
        return new Array(this.number).fill(this.name).join('\n');
    }
};

function bindCallbacks(): Callbacks {
    const r: Callbacks = {};
    const theThis: This = {
        name: 'John1',
        context: {},
        number: 0
    }

    for (const key in callbacks) {
        const fn = callbacks[key].bind(theThis);
        r[key] = fn;
    }

    return r;
}

function test() {
    const callbacks = bindCallbacks();
    callbacks.multipleNames();
}

test();

I can't make the Fn type have an any type because I will lose the IntelliSense when creating the original functions, and trying to add an any type on this on the modified functions won't work either.



Solution 1:[1]

when you are declaring callbacks constant, you are declaring a function called multipleNames with no arguments. but in Fn type you declared the following:

type Fn = (this: This, ...args: any[]) => any | Promise<any>;

so you are forcing your functions to have an argument of type of This, witch you have not provided for multipleNames function.

you can declare your type as the following so you won't get error you mentioned:

type Fn = (this?: This, ...args?: any[]) => any | Promise<any>;

making this and args, optional arguments

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arshia Moghaddam