Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda