Skip to content

error TS1057, when resolving an async function with a #13128

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
eschwartz opened this issue Dec 22, 2016 · 2 comments
Closed

error TS1057, when resolving an async function with a #13128

eschwartz opened this issue Dec 22, 2016 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@eschwartz
Copy link

eschwartz commented Dec 22, 2016

TypeScript Version: 2.2.0-dev.20161221

Code

Use Case A: Fails with async keyword, and any lookup type

interface SomeObj {
  stringProp: string;
  anyProp: any;
}

async function getAnyProp<TObj extends SomeObj>(obj:TObj):Promise<TObj['anyProp']> {
  return Promise.resolve(obj.anyProp);
}

Expected behavior:

Code should compile successfully

Actual behavior:

Code does not compile. Emits an error:

(6,16): error TS1057: An async function or method must have a valid awaitable return type.

Where (6,16) is the location of getAnyProp

Use case B: Succeeds without async keyword

Note that the code will compile if you remove the async keyword:

function getAnyProp<TObj extends SomeObj>(obj:TObj):Promise<TObj['anyProp']> {
  return Promise.resolve(obj.anyProp);
}
// compiles successfully

Use case C: Fails with async keyword, and generic passed to Promise.resolve

I also get a strange error if I pass a generic to Promise.resolve:

async function getAnyProp<TObj extends SomeObj>(obj:TObj):Promise<TObj['anyProp']> {
  return Promise.resolve<TObj['anyProp']>(obj.anyProp);
}

Results in:

(6,16): error TS1057: An async function or method must have a valid awaitable return type.
(7,10): error TS1059: Return expression in async function does not have a valid callable 'then' member.

Again, if I remove the async keyword, this example compiles successfully.

Use Case D: Succeeds with string lookup type

The issue seems only to occur if the resolved lookup type is of type any.

This code, for example, compile successfully.

interface SomeObj {
  stringProp: string;
  anyProp: any;
}

async function getStringProp<TObj extends SomeObj>(obj:TObj):Promise<TObj['stringProp']> {
  return Promise.resolve<TObj['stringProp']>(obj.anyProp);
}

It will also compile successfully with number, boolean, void, or {}.

Use Case E: Succeeds without generic

If I do not use generics, the code compiles successfully.

For example:

interface SomeObj {
  anyProp: any;
}

async function getAnyProp():Promise<SomeObj['anyProp']> {
  return Promise.resolve<SomeObj['anyProp']>('foo');
}

Thank you for your help! I'm really loving the new lookup types.

@mhegazy mhegazy added the Bug A bug in TypeScript label Dec 22, 2016
@mhegazy mhegazy added this to the TypeScript 2.2 milestone Dec 22, 2016
@rbuckton
Copy link
Contributor

This is due to the fact that an IndexedType in our type system that points to any does not itself have the TypeFlags.Any flag, so it fails one of the conditions for checking for a "thenable" type. However, when we compare the type to Thenable, our type checking logic treats TObj['anyProp'] as any, which is why it is failing.

I'm testing a fix for this now.

@eschwartz
Copy link
Author

Wow, you guys rock.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Dec 22, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
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

3 participants