Skip to content
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

Narrowing generic union function argument doesn't narrow return type based on generic #57190

Closed
1 task done
zachstence opened this issue Jan 26, 2024 · 3 comments
Closed
1 task done
Labels
Duplicate An existing issue was already created

Comments

@zachstence
Copy link

zachstence commented Jan 26, 2024

Acknowledgement

  • I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.

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

const f = <T extends 'a' | 'b'>(t: T): T extends 'a' ? 'x' : 'y' => {
  if (t === 'a') return 'x' // Type '"x"' is not assignable to type 'T extends "a" ? "x" : "y"'. (2322)
  return 'y' // Type '"y"' is not assignable to type 'T extends "a" ? "x" : "y"'. (2322)
}

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.

@zachstence
Copy link
Author

I have also taken a look at #7294 and #13995, but I think this issue is different from those. However I may be overlooking something inherently wrong with the code, and would love some insight here.

@MartinJohns
Copy link
Contributor

Duplicate of #33912.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 26, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants