Skip to content
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

Allows type guards with switch statement (need help..) #2229

Closed
wants to merge 3 commits into from
Closed

Allows type guards with switch statement (need help..) #2229

wants to merge 3 commits into from

Conversation

bsteephenson
Copy link

[Sorry.. screwed up rebasing..]
Hi. This PR resolves issue #2214 allowing the following code to work

function logNumber(v: number) { console.log("number:", v); }
function logString(v: string) { console.log("string:", v); }

function foo1(v: number|string) {
    switch (typeof v) {
        case 'number':
            logNumber(v);
            break;
        case 'string':
            logString(v);
            break;
        default:
            throw new Error("unsupported type");
    }
}

I need some help (I'm kinda new to this..)

  1. TypeOfExpression doesn't have a member "text" but the Javascript equivalent does. What can I cast TypeOfExpression to to get access to "text"?
  2. I'm not sure where I should be writing tests.. (Or what a .types file is)
  3. This might be too permissive. In the previous example, if I were to have a case 'Watermelon' it would still work. Should I check to see that the type is a subset of v's type?

Thank you!

@msftclas
Copy link

msftclas commented Mar 6, 2015

Hi @bsteephenson, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!

This seems like a small (but important) contribution, so no Contribution License Agreement is required at this point. Real humans will now evaluate your PR.

TTYL, MSBOT;

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants