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

Enum type inference #51933

Closed
4 of 5 tasks
zolomatok opened this issue Dec 17, 2022 · 3 comments
Closed
4 of 5 tasks

Enum type inference #51933

zolomatok opened this issue Dec 17, 2022 · 3 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@zolomatok
Copy link

zolomatok commented Dec 17, 2022

Suggestion

πŸ” Search Terms

enum type inference
enum inference
infer enum type from value
short form enum values
enum case inference
enum without type name

βœ… Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Swift allows for omitting the type name of the enum after the variable has already been declared as the enum's type.

For example...

enum Planet {
    case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}

... can be used as:

let planet = Planet.earth
planet = .mars

instead of

let planet = Planet.earth
planet = Planet.mars

πŸ“ƒ Motivating Example

We often have longer enum type names and omitting them leads to much cleaner looking and more legible code.
Consider that instead of:

enum CompassPoint { north, east, south, west }

let direction = CompassPoint.north

switch(direction as CompassPoint) { 
    case CompassPoint.north: 
        console.log("Lots of planets have a north")
        break;
    case CompassPoint.east:
        console.log("Watch out for penguins")
        break
    case CompassPoint.south:
        console.log("Where the sun rises")
        break
    case CompassPoint.west:
        console.log("Where the skies are blue")
        break
}

we can write:

enum CompassPoint { north, east, south, west }

let direction = CompassPoint.north

switch(direction as CompassPoint) { 
    case .north: 
        console.log("Lots of planets have a north")
        break;
    case .east:
        console.log("Watch out for penguins")
        break
    case .south:
        console.log("Where the sun rises")
        break
    case .west:
        console.log("Where the skies are blue")
        break
}

Even a simple assignment looks much nicer this way. Instead of

planet = isBlue ? Planet.earth : Planet.mars

we can have

planet = isBlue ? .earth : .mars
@MartinJohns
Copy link
Contributor

Duplicate of #45956.

[x] This could be implemented without emitting different JS based on the types of the expressions

This was mistakenly checked. This requires to emit different JavaScript, otherwise your code wouldn't work anymore.

@zolomatok
Copy link
Author

Oh dang, you're right, I missed that!

@ahejlsberg ahejlsberg added Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision labels Dec 17, 2022
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants