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

147
Visualizações
What is the difference between these function signatures in TypeScript?

Update: Besides the below solution, setting strictBindCallApply to true for TS compiler allows .bind to preserve function signatures, which is the easiest way to fix this issue.

I'm working on an old codebase that's the definition of callback hell, so to make things easier I'm promisifying some methods on a class. I made a PromiseObject type to hold the promisified methods, defined as

export type PromiseObject = {
  [fn: string]: (...args: any) => Promise<any>
}

And then in the class I started with

promises: PromiseObject = {
   fn1: util.promisify(this.fn1)
}

Which worked great and TypeScript inferred the parameter and return types for the promisified function. But then I had a method of an object that would be created during execution of a method on the main class, so I promisified it like this:

promises: PromiseObject = {
   fn1: util.promisify(this.fn1)
   fn2: (provider: IdentityProvider, user: AssertedUser) =>
            util.promisify(provider.getGroups)(user)
}

And TypeScript complained because this fn2 didn't match the [fn: string]: (...args: any) => Promise<any> signature. I don't really understand how fn1 and fn2 have meaningfully different signatures; both of them take ...args: any and both return Promise<any>. fn2 just has an extra step before it returns its promise. What am I missing?

UPDATE T.J. Crowder had it right, I didn't need PromiseObject at all, removed that part and everything's working.

After removing, I also ensured proper binding with the following (see TS playground):

import * as util from 'util'

type AssertedUser = {
    username: string
}
type IdProvider = {
    getGroups: (user: AssertedUser, cb: (err: any, groups: number[]) => void) => void
}

class MyClass {
    fn1(arg: string, cb: (err: any, res: string) => void) {
        cb(null, arg)
    }
    private promises = {
        fn1: this.binder(util.promisify(this.fn1), this),
        fn2: (user: AssertedUser, provider: IdProvider) => this.binder(util.promisify(provider.getGroups), provider)(user)
    }
    private binder<T, U>(promiseFn: (arg: T) => Promise<U>, thisArg: any) {
        const bound = promiseFn.bind(thisArg)
        return bound as typeof promiseFn
    }
}
about 4 years ago · Juan Pablo Isaza
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