GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Mutability widening for literal types by DanielRosenwasser · Pull Request #6554 · microsoft/TypeScript

Conversation

@DanielRosenwasser

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) commented Jan 21, 2016

Copy link
Copy Markdown
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 string at appropriate binding locations. This widening to string 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:

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 string even for a mutable binding:

declare function f(): "Foo" | "Bar";

// type: "Foo" | "Bar"
let a = f();

Fundamental changes to string literals and string literal types are:

  1. String literal types come in two flavors: fresh and stale (neither is "better" - think day old pizza).
  2. Two string literal types with the same content are always equal, regardless of freshess.
  3. A string literal _always_ starts off with a fresh string literal type, regardless of contextual typing.

Changes to the widening process are as follows:

  1. Variable-like declarations with no type annotations now go through an intermediate mutability-aware widening process.
  2. The standard widening process now widens all fresh literal types to string.

Mutability-aware widening is fairly simple:

  1. For mutable bindings (like var and let declarations), a singleton literal type is widened to string, regardless of freshness.
  2. For immutable bindings (like const declarations):
    1. A string literal type is stripped of freshness.
    2. A union's type's string literal type constituents are stripped of freshness.

Changes to best-common-type selection are as follows:

  1. If all constituents are string-like, then the resulting type is string.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

domoritz and ConquestArrow reacted with heart emoji
All reactions
  • ❤️ 2 reactions
@msftclas Microsoft Contribution License Agreements (msftclas) added the cla-not-required label Jan 21, 2016
@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Jan 21, 2016

Copy link
Copy Markdown
Member Author

This looks kind of untenable to review. I'm gonna rebase this so people can go commit by commit.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Daniel Rosenwasser (DanielRosenwasser) added 22 commits January 20, 2016 16:32
@DanielRosenwasser
Start all strings off with fresh literal types.
107f0e9
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.
@DanielRosenwasser
Only perform mutability widening when the user does not supply a type…
c349198
… annotation.
@DanielRosenwasser
Only perform mutability widening when an initializer is present so th…
c1f49a8
…at contextual typing can succeed.

Fixes breakage found in 'tests/cases/fourslash/quickInfoForOverloadOnConst1.ts'
@DanielRosenwasser
Consider 'string' to be a common supertype when all constituents are …
1715f52
…string literal types.
@DanielRosenwasser
Removed unused functions.
9ae177f
@DanielRosenwasser
Correctly check for if a declaration is const, and strip freshness of…
1325834
…f of string literal types in unions.
@DanielRosenwasser
Removed ad-hoc string-like checking for type assertions, switch/cases…
a46f923
…, and equality comparisons.
@DanielRosenwasser
Let 'isRelatedTo' fall down to the error case.
2b3e119
@DanielRosenwasser
Accepted symbol baselines.
df20678
@DanielRosenwasser
Accepted type baselines.
ba04c06
@DanielRosenwasser
Accepted remaining baselines.
b709375
@DanielRosenwasser
Added tests.
4239f64
@DanielRosenwasser
Added tests.
fe514a4
@DanielRosenwasser
Added motivating tests.
d38eed7
@DanielRosenwasser
Added tests with default initializers.
da0b66d
@DanielRosenwasser
Added tests for string literals within arrays.
ca8f7b8
@DanielRosenwasser
Added tests where properties are unions of literal types.
2c4abb5
@DanielRosenwasser
Added tests for string literal types and imports.
491707d
@DanielRosenwasser
Renamed a few tests.
064c5b3
@DanielRosenwasser
Accepted baselines.
efc7cb1
@DanielRosenwasser
Corrected fourslash test.
259e62d
@DanielRosenwasser
Fixed fourslash test.
0319f0f
@DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser) force-pushed the literalTypeWidening branch from cd0b085 to 0319f0f Compare January 21, 2016 01:56
@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Jan 21, 2016

Copy link
Copy Markdown
Member Author

I've rebased so there are batches of commits. Here's the order

  1. Changes to the type checker.
  2. Baseline acceptances for symbols, types, and then error & output files.
  3. Addition of new tests.
  4. Baseline acceptance of for tests.
  5. Correction of fourslash tests.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JsonFreeman

Jason Freeman (JsonFreeman) commented Jan 21, 2016

Copy link
Copy Markdown
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?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Jan 21, 2016

Copy link
Copy Markdown
Member Author

What flavor of string literal type does a string type literal introduce?

Fresh. Thanks for the question, I've amended the issue.

Does a readonly property initialization strip a string literal of its freshness?

At this point I believe that will be the case, but I will need to discuss it with Anders Hejlsberg (@ahejlsberg).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) added the Domain: Literal Types Unit types including string literal types, numeric literal types, Boolean literals, null, undefined label Jan 22, 2016
@JsonFreeman Jason Freeman (JsonFreeman) mentioned this pull request Jan 29, 2016
String literal types #5185
Merged
@domoritz Dominik Moritz (domoritz) mentioned this pull request Feb 7, 2016
Replace type string with string literals. vega/vega-lite#1080
Closed
@erichillah Eric Hillah (erichillah) mentioned this pull request Feb 15, 2016
Strict object literal check fails when infered to type containing string literal type #7085
Closed
@domoritz Dominik Moritz (domoritz) mentioned this pull request Feb 15, 2016
Reduce redundancy in generated schema vega/vega-lite#1011
Closed
1 of 3 tasks
@tinganho Tingan Ho (tinganho) mentioned this pull request Feb 22, 2016
Const strings should default to string literal type #7180
Closed
@sandersn Nathan Shively-Sanders (sandersn) mentioned this pull request Feb 23, 2016
String literal types in complex type expressions #7199
Closed
@weswigham Wesley Wigham (weswigham) mentioned this pull request Mar 11, 2016
Prototype: Numeric Literal Types #7480
Closed
@malibuzios malibuzios mentioned this pull request Mar 28, 2016
Suggestion: Type Property type #1295
Closed
@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Mar 30, 2016

Copy link
Copy Markdown
Member Author

This was nice to experiment with. 😃

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) closed this Mar 30, 2016
@ahejlsberg Anders Hejlsberg (ahejlsberg) mentioned this pull request Sep 3, 2016
Always use literal types #10676
Merged
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Sep 9, 2016
Design Meeting Notes, 9/9/2016 #10826
Closed
@mhegazy
Mohamed Hegazy (mhegazy) deleted the literalTypeWidening branch November 2, 2017 21:02
@microsoft Microsoft (microsoft) locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

Domain: Literal Types Unit types including string literal types, numeric literal types, Boolean literals, null, undefined

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@DanielRosenwasser @JsonFreeman @msftclas
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.