Closed
Description
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.