Skip to content

Cannot await on interfaces extending Promise<any> in TypeScript 2.1 RC #12292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
thomasboyt opened this issue Nov 16, 2016 · 8 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@thomasboyt
Copy link

For context: I ran into this bug using Knex and its type definition in TypeScript 2.1 RC. Knex's type definitions use an extended Promise interface for every value returned from a query, to allow for further chaining. Unfortunately, it looks like async/await in TypeScript does not support this.

I found several references to fixed issues and regressions in prior versions of TypeScript, but could not find an open issue for the current version. Apologies if this is a duplicate issue.

TypeScript Version: typescript@rc (2.1.1)

Code

main.ts:

interface PromiseExtender extends Promise<any> {
}

function getPromiseExtender(): PromiseExtender {
  return new Promise((resolve, reject) => {});
}

async function main() {
  await getPromiseExtender();  // error: Operand for 'await' does not have a valid callable 'then' member.
}

tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "es6", "es2015.promise", "es2015.iterable", "scripthost"],
    "module": "commonjs",
    "sourceMap": true,
    "strictNullChecks": true,
    "noImplicitAny": true
  }
}

Runnable test case: https://github.com/thomasboyt/typescript-await-promise-bug-test-case

Expected behavior: TypeScript allows awaiting interfaces extending Promise<any>.

Actual behavior: TypeScript displays the error Operand for 'await' does not have a valid callable 'then' member..

My current workaround for this is to just await (promiseExtendingObject as any), so it's not the end of the world. Hopefully can be fixed before 2.1 is released, though :)

@Jessidhia
Copy link

Would interface PromiseExtender<T> extends Promise<T> {} work?

@dead-claudia
Copy link

dead-claudia commented Nov 19, 2016

@Kovensky It shouldn't be necessary - ES async/await operates on thenables, not just promises.

@thomasboyt
Copy link
Author

Ran into another library with this issue: supertest-as-promised, which adds a then method to an existing interface (with the slight indirection of going through Bluebird, since that's the underlying promise implementation that library uses: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/supertest-as-promised/supertest-as-promised.d.ts).

@itaysabato
Copy link

Same deal with superagent.
It doesn't seem like it matters what is used as the generic parameter...

@rbuckton
Copy link
Contributor

rbuckton commented Dec 8, 2016

Fixed in #12743

@rbuckton rbuckton added the Fixed A PR has been merged for this issue label Dec 8, 2016
@rbuckton rbuckton closed this as completed Dec 8, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Dec 8, 2016

Fix should be in typescript@next later tonight. Please give it a try and let us know if you are still running into issues.

@mhegazy mhegazy added this to the TypeScript 2.1.5 milestone Dec 8, 2016
@dead-claudia
Copy link

dead-claudia commented Dec 10, 2016 via email

@itaysabato
Copy link

@mhegazy seems to be fine now, thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

6 participants