-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Functions operating on union types with a generic type parameter do not compile return type #25915
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
The original code definitely can't work: there is no relationship between T and the argument. If you change the argument to type T like this, then narrowing no longer works; there's an existing suggestion #20375 about this. Even if that were fixed, the compiler can't rule out that elsewhere in the program, you define a subclass of |
Hey @mattmccutchen ! Thank you for the great insight. My follow up is: do we expect the workaround I have in place to stop working in the future? I believe the issue with the latter half of your comment has to do with variance? I feel that this space in TS has a lot of open issues, so I'm not sure what I can add here. However, I do understand how complex that can make the compiler and how few bugs there are. I was trying to achieve this: I explored a few options for this:
This above approach works great, originally I had this returning a string index signature and having each case have an inner function that correctly typechecks the object. (Also, yes I noticed the lack of narrowing in changing the argument type, interesting to see the suggestions in #20375 thank you!) |
I don't have a definite answer about the workaround. I expect that the return type will continue to simplify as you expect at call sites that set |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version:
originally seen in 2.9.2
reproduced in 3.1.0-dev.20180724
Search Terms:
Code
Expected behavior:
This code should compile. The union type should be narrowed down by the switch statement and the returned type should behave correctly. Interestingly, this compilation error can be worked around. If you supply a lookup that looks like the below and update the function declaration, the expected behavior does occur:
However, this workaround only works if the PickASTKeys is there, if I do not include the PickASTKeys in the lookup and put that in the return type, I again get compilation errors. I have included that example in the playground list
Actual behavior:
3 errors are received:
Playground Link:
Link with failing compiler
Link with workaround that does not compile
Link with workaround
Related Issues:
I honestly looked, but the namespace for this type of issue is fairly overloaded. I would not find anything within a reasonable timeframe, but I might have been looking for the wrong keywords.
The text was updated successfully, but these errors were encountered: