Skip to content

Don't emit 'variable x is used before being assigned', when the variable is assigned in every possible case of a switch statement. #34497

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

Closed
5 tasks done
evert opened this issue Oct 15, 2019 · 6 comments
Labels
Duplicate An existing issue was already created Fixed A PR has been merged for this issue

Comments

@evert
Copy link

evert commented Oct 15, 2019

Search Terms

  • used before assigned switch

Suggestion

The following source returns Variable 'hi' is used before being assigned.:

type Foo = 'a' | 'b';

function test(foo: Foo) {
    let hi: string;
    switch (foo) {
        case 'a':
            hi = 'hello';
            break;
        case 'b':
            hi = 'world';
            break;
    }
    console.log(hi);
}

It seems like every branch assigns the variable, but maybe I there's a secret branch I can't see.

Use Cases

I'm forced to add a ! or a default. Neither are really optimal. If type Foo changes later to include a third case, at that point I would like typescript to complain about this error. Effectively I would like to be statically guarded to the Foo type changing.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@jack-williams
Copy link
Collaborator

Duplicate of #18362 or #30000?

@evert
Copy link
Author

evert commented Oct 15, 2019

Those two seem similar, but they both deal with enum. I don't know TS enough to say if it's the exact same problem.

@j-oliveras
Copy link
Contributor

This works with version 3.7-beta.

@jack-williams
Copy link
Collaborator

Good spot @j-oliveras

@RyanCavanaugh I added fixed labels to the associated issues I posted above --- mainly because one was locked and I could only label it. Just letting you know so you can amend my labels.

@evert
Copy link
Author

evert commented Oct 16, 2019

Wow awesome! excellent news

@ahejlsberg ahejlsberg added Duplicate An existing issue was already created Fixed A PR has been merged for this issue labels Oct 16, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants