-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Can't use type aliases or conditional types resolving to Promise for async/await return types #27987
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
Comments
As a workaround, can you write |
Yes, that works for my use case. Should have thought of it myself, thanks! |
just had same issue, this looks to be regression in latest TS version (3.1.3) in prev versions this results in compilation error (as far as I can see). Work around indeed works, but it would be better if this will break compilation, so will know immediately this is not supported |
I just had this problem, and I did not think to place the whole conditional Here's my playground example. Thanks for the work around, I'll use that. A fix for this would be nice, so it works for people trying it for the first time (f.e. me). |
This is behaving by design. ES5 and earlier runtimes did not have a native @swar30: Are you suggesting that |
@rbuckton I would say that compile time and run time should be aligned. Actually I would have preferred it to work in both cases, meaning been able to use above type expressions and have the generated code actually work. From what I saw, what happens is that while I would expect the types to be erased in compilation, in this specific case they are not, and there is still reference in transpiled code to |
It looks like this is still a problem, and furthermore that the workaround suggested by @.weswigham doesn't work as of Typescript 3.9.2:
Expected behavior: Actual behavior:
Even when I change the return type to Let me know if:
|
Is there a workaround for this in 2022? In my example, I have a module that makes API requests, and I'd love to refactor the return type as a Promise that resolves to either an Error or the Response type. export type GRPCResult<T> = Promise<FP.Either<Error, T>>;
async function postFoo(call: CF.API.PostFooCall): GRPCResult<PostFooResponse> {
// ...
} |
Looks like the workaround is to create a variable that aliases the const GRPCResult = Promise; // <---- workaround
// eslint-disable-next-line @typescript-eslint/no-redeclare
export type GRPCResult<T> = Promise<FP.Either<Error, T>>;
async function postFoo(call: CF.API.PostFooCall): GRPCResult<PostFooResponse> {
// ...
} It's annoying and concerning that this triggers a lint rule for redeclaring a variable, but I'm willing to live with this for now until this issue is fixed properly... |
@akatechis it should work without workaround when you set your target to ES2015 or later |
TypeScript Version: 3.1
Search Terms:
Code
Expected behavior:
No error.
Actual behavior:
Type 'P' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
Type 'T extends number ? Promise : Promise' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
Playground Link:
Link
Related Issues:
The text was updated successfully, but these errors were encountered: