-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Mutability widening for literal types #6554
Conversation
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
I've rebased so there are batches of commits. Here's the order
|
What flavor of string literal type does a string type literal introduce? Does a readonly property initialization strip a string literal of its freshness? |
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. |
This was nice to experiment with. 😃 |
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
string
at appropriate binding locations. This widening tostring
occurs 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
string
even 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:
var
andlet
declarations), a singleton literal type is widened tostring
, regardless of freshness.const
declarations):Changes to best-common-type selection are as follows:
string
.