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 — consider allowing exhaustive switch statement · Issue #6155 · microsoft/TypeScript

consider allowing exhaustive switch statement #6155

New issue
Copy link
New issue
Copy link
Closed
Closed
consider allowing exhaustive switch statement#6155
Copy link
Labels
FixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
TypeScript 2.0

Description

@zpdDG4gta8XKpMCd
ZpdDG4gta (zpdDG4gta8XKpMCd)
opened on Dec 18, 2015
Issue body actions
type foo = 'hey' | 'nay';
let value : foo = 'hey';
switch (value) {
    case 'hey': doThis(); break;
    case 'nay': doThat(); break;
    default: void; // <-- syntax TBD that indicates: 1. no other than known options are allowed 2. all known options must be considered
}
Reacted by Tingan Ho, Ceyhun Can Ulker, Daniel M., Kris Kaczor and Bruno Lemos

Activity

  1. zpdDG4gta8XKpMCd commented on Dec 18, 2015

    @zpdDG4gta8XKpMCd
    ZpdDG4gta (zpdDG4gta8XKpMCd)
    on Dec 18, 2015
    Author
    More actions

    (it's up to the developer to take the responsibility for possible contract violations at runtime)

  2. weswigham commented on Dec 18, 2015

    @weswigham
    Wesley Wigham (weswigham)
    on Dec 18, 2015
    Member
    More actions

    The default branch in your example can be triggered when value is undefined or null as both let value : foo = null; and let value : foo = undefined; are valid for type foo.

  3. zpdDG4gta8XKpMCd commented on Dec 18, 2015

    @zpdDG4gta8XKpMCd
    ZpdDG4gta (zpdDG4gta8XKpMCd)
    on Dec 18, 2015
    Author
    More actions

    since we are going exhaustive here, and there is no way to ask the type system to prevent those 2 from popping up, let's require the developer to take care of them explicitly:

    case undefined: doSomethingElse(); break;
    case null: void; // <-- instructing the compiler to chill
  4. zpdDG4gta8XKpMCd commented on Dec 18, 2015

    @zpdDG4gta8XKpMCd
    ZpdDG4gta (zpdDG4gta8XKpMCd)
    on Dec 18, 2015
    Author
    More actions

    slippery moment here is that the undefined is just a variable, not a literal, but hey, why would you want to mess with it? anyway the emitter can take care of it:

    case void undefined: doSomethingElse(); break;

    By the way, why wouldn't TypeScript stop this nonsense and straiten things up by proclaiming undefined a literal and thus preventing it from being assigned?

  5. zpdDG4gta8XKpMCd commented on Dec 18, 2015

    @zpdDG4gta8XKpMCd
    ZpdDG4gta (zpdDG4gta8XKpMCd)
    on Dec 18, 2015
    Author
    More actions

    On top of that please admit that Undefined is a real type that can be referenced as well as Null. It's never a good idea to depart from what the real world is (I mean JavaScript here) by trying to model it using wrong entities (void type in TypeScript).

  6. RyanCavanaugh commented on Dec 18, 2015

    @RyanCavanaugh
    Ryan Cavanaugh (RyanCavanaugh)
    on Dec 18, 2015
    Member
    More actions

    TypeScript should stop this nonsense and straiten things up by proclaiming undefined a literal and thus prevent it from being assigned.

    This is already the case in strict mode.

    As for the null and undefined types, I think we want to keep their names "off limits" until we address nullability as a real language feature and understand the desired semantics there.

  7. Mohamed Hegazy (mhegazy)
    added
    SuggestionAn idea for TypeScriptAn idea for TypeScript
    In DiscussionNot yet reached consensusNot yet reached consensus
    on Feb 19, 2016
  8. mhegazy commented on Feb 19, 2016

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on Feb 19, 2016
    Contributor
    More actions

    with #7140 in place we should be able to make better decisions here.

  9. zpdDG4gta8XKpMCd commented on Feb 19, 2016

    @zpdDG4gta8XKpMCd
    ZpdDG4gta (zpdDG4gta8XKpMCd)
    on Feb 19, 2016
    Author
    More actions

    I am super excited to welcome non-nullables, but exhaustive switches are a different beast isn't it?

  10. ahejlsberg commented on Jun 29, 2016

    @ahejlsberg
    Anders Hejlsberg (ahejlsberg)
    on Jun 29, 2016
    Member
    More actions

    Checking for exhaustive switch statements is now possible with #9407.

    Reacted by Vlad GURDIGA
  11. zpdDG4gta8XKpMCd commented on Jun 29, 2016

    @zpdDG4gta8XKpMCd
    ZpdDG4gta (zpdDG4gta8XKpMCd)
    on Jun 29, 2016
    Author
    More actions

    best day ever! thanks a bunch

  12. Mohamed Hegazy (mhegazy)
    added
    FixedA PR has been merged for this issueA PR has been merged for this issue
    and removed
    In DiscussionNot yet reached consensusNot yet reached consensus
    on Jun 29, 2016
  13. Mohamed Hegazy (mhegazy)
    added this to the TypeScript 2.0 milestone on Jun 29, 2016
  14. microsoft
    locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      • TypeScript 2.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.