Narrowing generic union function argument doesn't narrow return type based on generic #57190
Closed
1 task done
Labels
Duplicate
An existing issue was already created
Acknowledgement
Comment
I am using the generic "Other" template for this issue because I am not sure if it is a bug, I am misunderstanding something, or it would be a feature request. I would be happy to close and reopen with another template if necessary!
Reproduction:
https://www.typescriptlang.org/play?ts=5.4.0-dev.20240126#code/MYewdgzgLgBAZjAvDAPAFRgUwB5U2AEwhgHIBDEmAH1ICMSA+ACigC4Y0BKdjHPQ4uUoB+UtkrsSAT0qIGMAN4BYAFAwYASwQskiZEM4wATpigBXI2DElV6k+cukZqgL6qgA
Expected behavior:
Compiles without error, and function return type is narrowed by control flow in function body checking type of argument
Actual behavior:
Type '"x"' is not assignable to type 'T extends "a" ? "x" : "y"'.(2322)
Type '"y"' is not assignable to type 'T extends "a" ? "x" : "y"'.(2322)
Notes:
This is a minimal reproduction of a problem I came across when trying to do something similar with class generics and fields. I would be happy to provide some more detail about why this would be useful if necessary.
The text was updated successfully, but these errors were encountered: