Pavel
Pavel

Reputation: 2554

Typescript error when function return type is Promise<{ then: () => void }>

I cant declare function, that returns promise to Derivable. Reason is next:

Minimal exmaple in playground

interface Some {
  then(callback);
}

// Error: the return type of an async function must either be a valid promise or must not contain a callable 'then' member.
async function foo(): Promise<Some> {
  return null;
}

Is it bug in typescript? Is any workaround awailable? I cant use async/await because of this.

Upvotes: 0

Views: 2221

Answers (2)

Bergi
Bergi

Reputation: 664246

No, this is not a bug in Typescript. It's just fundamentally impossible to fulfill a promise with a thenable (an object with a then method), as it tries to assimilate it when resolving. As a workaround, you can wrap the Derivable in an extra object, as a proper solution I would recommend renaming then to chain.

Upvotes: 5

Fabio Carpinato
Fabio Carpinato

Reputation: 1181

I think the problem is that typescript cannot recognize the different signature of the method then because you kinda overrided it in the interface.

Upvotes: 0

Related Questions