Mutability widening for literal types#6554
Closed
DanielRosenwasser wants to merge 22 commits intomasterfrom
Closed
Mutability widening for literal types#6554DanielRosenwasser wants to merge 22 commits intomasterfrom
DanielRosenwasser wants to merge 22 commits intomasterfrom
Conversation
Member
Author
|
This looks kind of untenable to review. I'm gonna rebase this so people can go commit by commit. |
All string literals will now have their types inferred as string literal types with an associated "freshness". When a string is bound to a "read-only" declaration, freshness associated with the type is lost during widening. When a string is bound to a mutable declaration, it is unconditionally widened to 'string. In any other context where widening takes place, any fresh string literal type is widened to 'string.
…at contextual typing can succeed. Fixes breakage found in 'tests/cases/fourslash/quickInfoForOverloadOnConst1.ts'
…string literal types.
…f of string literal types in unions.
…, and equality comparisons.
cd0b085 to
0319f0f
Compare
Member
Author
|
I've rebased so there are batches of commits. Here's the order
|
Contributor
|
What flavor of string literal type does a string type literal introduce? Does a readonly property initialization strip a string literal of its freshness? |
Member
Author
Fresh. Thanks for the question, I've amended the issue.
At this point I believe that will be the case, but I will need to discuss it with @ahejlsberg. |
Merged
3 tasks
Member
Author
|
This was nice to experiment with. 😃 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR changes the way in which string literal types work today. It seeks to address some of the major issues brought up in #6167. The basic gist is that all string literals start off with string literal types and are appropriately widened to
stringat appropriate binding locations. This widening tostringoccurs when the binding itself is mutable.A alternative (and competing) approach exists on #6196, where we infer string literal types only at select locations.
Here's a small sample of the current behavior:
However, when working with a union type of string literals, we won't automatically widen to
stringeven for a mutable binding:Fundamental changes to string literals and string literal types are:
Changes to the widening process are as follows:
string.Mutability-aware widening is fairly simple:
varandletdeclarations), a singleton literal type is widened tostring, regardless of freshness.constdeclarations):Changes to best-common-type selection are as follows:
string.