You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Convert to async function refactoring loses generic parameter #28529
then the return type becomes Promise<Response<Response<{ email: string }>>. The type argument provided to wrapResponse represents the type of its input, whereas the type argument provided to then represents the type of the fulfilled handler’s output. In the parseJson example and in Matt Bierner (@mjbvz)’s minimal example, those happen to be the same thing, but it’s purely coincidental.
I guess the best thing to do would be to move the type annotation to the return type if it was in a return expression:
or to a constant, if it was originally a constant:
asyncfunctionget(){constresponse=awaitPromise.resolve((undefined!));constresult: Response<{email: string}>=awaitwrapResponse(response);// ... other stuff I guess}
but I’m not sure what to do if the original function declaration or constant already had a type annotation that doesn’t agree... I guess just keep ignoring the type argument like we do now, as the type would be discarded in the end result of the expression anyway.
Ah, I see. I think I was confusing myself by using the same name for both type parameters in my example.
Is there a mechanism to refuse to do the refactoring and present a useful message explaining why? Maybe that would be the best option (or a dialog presenting the user with a choice of which type to use, along with a cancel button)?
There’s not, currently. If the refactor fails it simply isn’t shown as an option. I think annotating either the return type or the constant is reasonably correct here... the most important thing is that the inferred return type of the function doesn’t change because of the refactor, which this approach should ensure. Perhaps, though, we should bail out of the refactor if there’s an existing type error because of a conflict between a then or catch type argument and the contextual type of the expression it’s in, because we’re likely to erase that conflict during the refactor.
Activity
mjbvz commentedon Nov 14, 2018
typescript@3.2.0-dev.20181114
Simple repo:
7 remaining items
andrewbranch commentedon Feb 11, 2020
This is a bug, but the expected behavior described is wrong. To explain why, take this example:
The return type of
getisPromise<Response<{ email: string }>>. If we apply the refactor as suggested:then the return type becomes
Promise<Response<Response<{ email: string }>>. The type argument provided towrapResponserepresents the type of its input, whereas the type argument provided tothenrepresents the type of the fulfilled handler’s output. In theparseJsonexample and in Matt Bierner (@mjbvz)’s minimal example, those happen to be the same thing, but it’s purely coincidental.I guess the best thing to do would be to move the type annotation to the return type if it was in a return expression:
or to a constant, if it was originally a constant:
but I’m not sure what to do if the original function declaration or constant already had a type annotation that doesn’t agree... I guess just keep ignoring the type argument like we do now, as the type would be discarded in the end result of the expression anyway.
ianp commentedon Feb 12, 2020
Ah, I see. I think I was confusing myself by using the same name for both type parameters in my example.
Is there a mechanism to refuse to do the refactoring and present a useful message explaining why? Maybe that would be the best option (or a dialog presenting the user with a choice of which type to use, along with a cancel button)?
andrewbranch commentedon Feb 12, 2020
There’s not, currently. If the refactor fails it simply isn’t shown as an option. I think annotating either the return type or the constant is reasonably correct here... the most important thing is that the inferred return type of the function doesn’t change because of the refactor, which this approach should ensure. Perhaps, though, we should bail out of the refactor if there’s an existing type error because of a conflict between a
thenorcatchtype argument and the contextual type of the expression it’s in, because we’re likely to erase that conflict during the refactor.ianp commentedon Mar 24, 2020
❤️