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 — Narrow string and number types in literal equality checks by ahejlsberg · Pull Request #11587 · microsoft/TypeScript

Conversation

@ahejlsberg

@ahejlsberg Anders Hejlsberg (ahejlsberg) commented Oct 13, 2016 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

With this PR we narrow the string and number types as appropriate when they are compared with values of literal types.

function move(direction: "up" | "down") {
    // ...
}

function do1(command: string) {
    if (command === "up" || command === "down") {
        move(command);  // Narrowed to type "up" | "down"
    }
}

function do2(command: string) {
    switch (command) {
        case "up":
        case "down":
            move(command);  // Narrowed to type "up" | "down"
            break;
    }
}

function f1(x: number, y: 1 | 2) {
    if (x === 0 || x === y) {
        x;  // Narrowed to type 0 | 1 | 2
    }
}

function f2(x: number | "foo" | "bar", y: 1 | 2 | string) {
    if (x === y) {
        x;  // Narrowed to type "foo" | "bar" | 1 | 2
        y;  // Narrowed to type "foo" | "bar" | 1 | 2
    }
}

Interestingly this found a bug in our scanner, pointing out that type 2 is not comparable to type 8.

Fixes #7447.
Fixes #10417.
Fixes #11306.

Sorry, something went wrong.

Uh oh!

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

HerringtonDarkholme, tinganho, normalser, aluanhaddad, robertknight, tekacs, malekpour, rictic, GoToLoop, SlurpTheo, and nikeee reacted with thumbs up emoji rictic and GoToLoop reacted with hooray emoji
All reactions
  • 👍 11 reactions
  • 🎉 2 reactions
Anders Hejlsberg (ahejlsberg) added 5 commits October 12, 2016 14:12
@ahejlsberg
Narrow string and number types in equality checks and switch cases
7497d81
@ahejlsberg
Fix bug in scanner
4c581de
@ahejlsberg
Properly narrow union types containing string and number
7dd64d3
@ahejlsberg
Accept new baselines
57a9a63
@ahejlsberg
Add tests
8bcb22c
@msftclas Microsoft Contribution License Agreements (msftclas) added the cla-not-required label Oct 13, 2016
mhegazy
Mohamed Hegazy (mhegazy) approved these changes Oct 13, 2016
View reviewed changes
@ahejlsberg
Anders Hejlsberg (ahejlsberg) merged commit 17c2ab2 into master Oct 13, 2016
@ahejlsberg
Anders Hejlsberg (ahejlsberg) deleted the narrowStringAndNumber branch October 13, 2016 21:00
@zpdDG4gta8XKpMCd

ZpdDG4gta (zpdDG4gta8XKpMCd) commented Oct 13, 2016

Copy link
Copy Markdown

i am not at the computer to check, but would it fix #11433 as well?

All reactions

Sorry, something went wrong.

Uh oh!

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

@mhegazy

Mohamed Hegazy (mhegazy) commented Oct 14, 2016

Copy link
Copy Markdown
Contributor

i am not at the computer to check, but would it fix #11433 as well?

nope. that is a different issue.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ahejlsberg Anders Hejlsberg (ahejlsberg) mentioned this pull request Oct 22, 2016
Not possible to compile latest Angular 2 due to literal types #11794
Closed
@normalser normalser mentioned this pull request Nov 11, 2016
Add support for defining: string - 'a' (string minus string literal type) #12182
Closed
@kitsonk Kitson Kelly (kitsonk) mentioned this pull request Nov 15, 2016
Migrate to TypeScript 2.1 dojo/meta#111
Closed
@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

1 more reviewer
@mhegazy Mohamed Hegazy (mhegazy) mhegazy approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

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

Assignees

No one assigned

Labels

None yet

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.

4 participants

@ahejlsberg @zpdDG4gta8XKpMCd @mhegazy @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.