Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

222
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda