-
Notifications
You must be signed in to change notification settings - Fork 13k
Keep returned (and yielded) literal types as const when their types using const
type variables
#56859
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
base: main
Are you sure you want to change the base?
Keep returned (and yielded) literal types as const when their types using const
type variables
#56859
Conversation
…sing `const` type variables
The TypeScript team hasn't accepted the linked issue #53813. If you can get it accepted, this PR will have a better chance of being reviewed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change seems sensible but we should probably review it at a design meeting.
…-vs-const-type-variables
My notes from the design meeting: This improves the apparent consistency so much that it's worth taking. The implementation needs to be right of course. To check that:
Also decide whether this is the right place to implement it; could be that it's better to put it in getWidenedType. (@weswigham brought this concern up but seemed to be believe that its current place is better.) |
@typescript-bot test top100 |
Heya @sandersn, I've started to run the diff-based top-repos suite on this PR at 78831c2. You can monitor the build here. Update: The results are in! |
Heya @sandersn, I've started to run the parallelized Definitely Typed test suite on this PR at 78831c2. You can monitor the build here. Update: The results are in! |
Heya @sandersn, I've started to run the regular perf test suite on this PR at 78831c2. You can monitor the build here. Update: The results are in! |
Heya @sandersn, I've started to run the diff-based user code test suite on this PR at 78831c2. You can monitor the build here. Update: The results are in! |
Hey @sandersn, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
@sandersn Here they are:
CompilerComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
tsserverComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
StartupComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
@sandersn Here are the results of running the user test suite comparing There were infrastructure failures potentially unrelated to your change:
Otherwise... Something interesting changed - please have a look. Details
|
Hey @sandersn, the results of running the DT tests are ready. |
@sandersn Here are the results of running the top-repos suite comparing Everything looks good! |
It looks like we decided in a design meeting that this is a good change, but it still needs more tests:
@Andarist do you want to finish up this PR by adding those tests? |
…-vs-const-type-variables
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes TypeScript's type inference for const type parameters by preserving literal types when functions with const
type parameters return or yield literal values. Previously, const type parameters weren't properly maintaining literal types in return statements and yield expressions.
Key changes:
- Fixed literal type preservation for return expressions in functions with const type parameters
- Fixed literal type preservation for yield expressions in generator functions
- Ensured const context is properly applied to returned and yielded values
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
File | Description |
---|---|
tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiersReturnsAndYields.ts |
Test cases covering const type parameters with returns, yields, and generator functions |
tests/baselines/reference/typeParameterConstModifiersReturnsAndYields.types |
Expected type baseline showing literal types are preserved |
tests/baselines/reference/typeParameterConstModifiersReturnsAndYields.symbols |
Expected symbol baseline for the test cases |
src/compiler/checker.ts |
Core compiler fix adding const context checks for returns and yields |
Comments suppressed due to low confidence (3)
@sandersn I added some new test cases here. Please take a look and see if those are satisfactory. |
fixes #56858
closes #53813