GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||
GeezerVison is showing github.com — Mutability widening for literal types by DanielRosenwasser · Pull Request #6554 · microsoft/TypeScript Conversation
Member
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 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: const HelloKind = "Hello"; // type: "Hello" let GoodbyeKind = "Goodbye"; // type: string const Kind_A = HelloKind; // type: "Hello" let Kind_B = HelloKind; // type: string // type: { name: string; } const Foo = { name: "Foo", }; interface Bar { barProp: "Bar"; } const myBar: Bar; // type: "Bar" const { barProp } = myBar; // type: string const { baz } = { baz: "bar" }; However, when working with a union type of string literals, we won't automatically widen to declare function f(): "Foo" | "Bar"; // type: "Foo" | "Bar" let a = f(); Fundamental changes to string literals and string literal types are:
Changes to the widening process are as follows:
Mutability-aware widening is fairly simple:
Changes to best-common-type selection are as follows:
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Member
Author
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. 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.
Daniel Rosenwasser (DanielRosenwasser)
force-pushed
the
literalTypeWidening
branch
from
cd0b085 to
0319f0f
Compare
Member
Author
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Contributor
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Member
Author
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Merged
1 of 3 tasks
Member
Author
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
ReviewersNo reviewsUh oh!
There was an error while loading. Please reload this page. AssigneesNo one assignedLabels
Domain: Literal Types
ProjectsMilestoneNo milestoneDevelopmentSuccessfully merging this pull request may close these issues. Uh oh!
There was an error while loading. Please reload this page. 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. | |||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |