Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!