Skip to content

Enum number return type is not type safe #36756

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
ismail-codinglab opened this issue Feb 12, 2020 · 4 comments
Closed

Enum number return type is not type safe #36756

ismail-codinglab opened this issue Feb 12, 2020 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@ismail-codinglab
Copy link

TypeScript Version: 3.8.0-dev.20200211

Search Terms:
Enum return type, enum strictly typing, enum type safety
Code

enum Hello{
  world
}

const sayHello = ():Hello => {
  return 2;
};
console.log(sayHello()); // 2

Expected behavior:
An exception/error that 2 is not one of the enum option
and that i should use the enum and not just a number value. Which in the programming world
is commonly known as Magic number

Actual behavior:
returns 2
Playground Link:
https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=8&pc=30#code/KYOwrgtgBAEsA28D2BvAsAKClA7kgTvACZQC8UAjJgL6aYDGSIAzgC5TMCGAnnIkmSgAKAJQAuPsjIA+KOixR8wVmHwgoAJgDcNHRkYsk8YADpkAcyFdeCZKJFaoAeieagA

Hackish workaround:

enum Hello{
  world = "1",
}

const sayHello = ():Hello => {
  return "2"; // <-- will give an error
};
console.log(sayHello()); // 2

But typescript should make our code more typesafe and give enums the power it should have in first place.

@AnyhowStep
Copy link
Contributor

AnyhowStep commented Feb 12, 2020

Number enums are not really enums at all. It's "by design", even if many people don't agree.

They were originally meant to just be useful aliases for bitmasks.

[Edit]
See: #32690

@ismail-codinglab
Copy link
Author

Enum numbers are by design meant to be not actually enums and string enums are by design actual enums?

I would just expect number enums to act the same as string enums

Hope you can help me out, it's giving different type restrictions depending on if the Enum is a number or string.

If many people feel the same then it's good to discuss about it

I think you would agree with me that the restrictions for string enums should also be reflected with number / default enums and will help the developer gain a more strictly typed environment

@AnyhowStep
Copy link
Contributor

AnyhowStep commented Feb 12, 2020

I don't like it, either. It is what it is, though.

But, yes, number and string enums have different behavior. Also, if you look at the JS emit for number and string enums, you'll see that they're also different.

It's worth noting that JS has no actual enum type. It's a TS concept. Just like namespace.


I just tell people to not use enums nowadays.

Just do something like,

const MyNumberEnum = {
  A : 123,
  B : 456,
} as const;
type MyNumberEnum = typeof MyNumberEnum[keyof typeof MyNumberEnum]

Or,

const MyStringEnum = {
  A : "a",
  B : "b",
} as const;
type MyStringEnum = typeof MyStringEnum[keyof typeof MyStringEnum]

It does make the emit uglier, though. In places, you'll see a union type, instead of MyNumberEnum

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Feb 13, 2020
@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
Projects
None yet
Development

No branches or pull requests

4 participants