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

const enum should allow "Infinity" as a valid name #6153

Closed
speigg opened this issue Dec 18, 2015 · 11 comments
Closed

const enum should allow "Infinity" as a valid name #6153

speigg opened this issue Dec 18, 2015 · 11 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this

Comments

@speigg
Copy link

speigg commented Dec 18, 2015

    declare const enum CameraDeviceFocusMode {
        Normal = 0,
        TriggerAuto = 1,
        ContinuousAuto = 2,
        Infinity = 3, // <- error: An enum cannot have a numeric name
        Macro = 4
    }
@RyanCavanaugh RyanCavanaugh added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Dec 18, 2015
@RyanCavanaugh
Copy link
Member

This is intentional because Infinity is also a valid enum value; we disallow any name that might be a value because the lookup object only works if no names are values and no values are names.

Recommended fix is to just use Infinite instead of Infinity

@speigg
Copy link
Author

speigg commented Dec 18, 2015

What is the lookup object exactly? Using the name Infinity as a property key is completely valid in javascript by the way.

@RyanCavanaugh
Copy link
Member

I missed that this enum was const. We don't need to enforce this rule for those.

@RyanCavanaugh RyanCavanaugh reopened this Dec 18, 2015
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed By Design Deprecated - use "Working as Intended" or "Design Limitation" instead labels Dec 18, 2015
@RyanCavanaugh
Copy link
Member

(Lookup object, see http://stackoverflow.com/a/28818850/1704166)

@RyanCavanaugh RyanCavanaugh changed the title Enum should allow "Infinity" as a valid name const enum should allow "Infinity" as a valid name Dec 18, 2015
@speigg
Copy link
Author

speigg commented Dec 18, 2015

Not enforcing that rule for const enum sounds great.

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels Jan 5, 2016
@RyanCavanaugh RyanCavanaugh added this to the Community milestone Jan 5, 2016
@RyanCavanaugh RyanCavanaugh added the Good First Issue Well scoped, documented and has the green light label Jan 5, 2016
@RyanCavanaugh
Copy link
Member

Approved even for non-const enums.

The suggested implementation was to skip any leading -, then see if the next (or first) character is a digit. Only names matching this rule should be subject to restriction. This should allow Infinity and NaN but still disallow names like 5.

@DanielRosenwasser
Copy link
Member

So what's the new rule now for numeric names? Same as before but make a special exception for "Infinity" and "NaN"?

Also @jmatthiesen asked about this a while back.

@RyanCavanaugh
Copy link
Member

Same as before but make a special exception for "Infinity" and "NaN"?

Correct (as well as -Infinity)

@mhegazy mhegazy added Bug A bug in TypeScript and removed Suggestion An idea for TypeScript labels Feb 24, 2016
@YuichiNukiyama
Copy link
Contributor

@RyanCavanaugh
Copy link
Member

Enum members need not be identifiers - they can be quoted much like the property names in object literals

@jbondc
Copy link
Contributor

jbondc commented May 9, 2016

Reported here too #3572

@mhegazy mhegazy modified the milestones: TypeScript 2.1, Community Sep 8, 2016
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Sep 8, 2016
@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
Bug A bug in TypeScript Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

6 participants