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

146
Views
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
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!