Skip to content

Allow enums in local scopes #3217

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
joshuacc opened this issue May 19, 2015 · 1 comment · Fixed by #3266
Closed

Allow enums in local scopes #3217

joshuacc opened this issue May 19, 2015 · 1 comment · Fixed by #3266
Assignees
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@joshuacc
Copy link

Currently the following is allowed:

enum Color { red, green, blue }

function fun(myColor) {
    if (myColor == Color.red) {
        console.log("awesome");
    } else {
        console.log("bummer");
    }
}

This compiles, but gives a warning:

function fun(myColor) {
    enum Color { red, green, blue } // Warning "Statement expected"

    if (myColor == Color.red) {
        console.log("awesome");
    } else {
        console.log("bummer");
    }
}

Especially when refactoring existing JavaScript code, there are cases where an enum would be useful, but wouldn't be used outside of a single function. In those cases, I think it makes sense to allow the enum to be defined inside of the function's scope.

@danquirk
Copy link
Member

Presumably this could be considered along with other kinds of types we would allow in a future 'local types' feature. Edited the title appropriately.

@danquirk danquirk changed the title Don't warn on function scoped enums? Allow enums in local scopes May 19, 2015
@danquirk danquirk added the Suggestion An idea for TypeScript label May 19, 2015
@mhegazy mhegazy added this to the TypeScript 1.6 milestone May 21, 2015
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label May 31, 2015
@RyanCavanaugh RyanCavanaugh added the Committed The team has roadmapped this issue label Jun 1, 2015
@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.
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants