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

keyof Enum #14106

Closed
sebdoucet opened this issue Feb 16, 2017 · 4 comments · Fixed by microsoft/TypeScript-Handbook#963
Closed

keyof Enum #14106

sebdoucet opened this issue Feb 16, 2017 · 4 comments · Fixed by microsoft/TypeScript-Handbook#963
Labels
Question An issue which isn't directly actionable in code

Comments

@sebdoucet
Copy link

sebdoucet commented Feb 16, 2017

Hi,

I ve seen a post over this particular suggestion, but I did not understant why (in typescript, not in js)

why this kind of behavior is not expected on keyof over enum:

enum Kind {
   none,
   bob
}

interface SomeJsonData {
    kind: keyof Kind;
    insteadOfKind: "none" | "bob";
}

Just saying that because it was my first reflex trying keyof over enums out of Partial or Readonly's.
Enum are not part of ecmascript (as I know) so keyof applyed to typescript should have a typescript specific behavior.

I also did not expect keyof returns number values, but if it does, still make more sense than current behavior.

Cheers.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Feb 16, 2017

Just to clarify, I think that the question is "Why do I get a bunch of string literals named after methods on numbers instead of "none" | "bob". Phrasing the question that way might clarify things a little.

An enum declaration actually declares two types:

  • The type of enum members (e.g. Kind.none, Kind.bob)
  • The type of the enum container itself (i.e. Kind)

Kind.none and Kind.bob each have a base type of Kind, which is really just a subtype of the number type, which has members of the Number type.

Kind on the other hand, is more or less an object with two members on it at runtime. Its type doesn't have a name, but you can still reference it by using the typeof operator (i.e. typeof Kind).

So if you're looking for "none" | "bob", you unfortunately want keyof typeof Kind.

@DanielRosenwasser DanielRosenwasser added the Question An issue which isn't directly actionable in code label Feb 16, 2017
@sebdoucet
Copy link
Author

sebdoucet commented Feb 16, 2017

This can have credit if:

  • enum where actual types
  • keyof was a real js key world

But, are really expecting this to append when you write keyof Enum ?

As I undertant, one part of your argumentation go again js (no Kind type in js).
But the other part justify the behavior of the keyof over the typescript to js translation.

If you really expect number prototype enumeration when you write that: keyof King... Sorry sir, I think you think weard

@sebdoucet
Copy link
Author

sebdoucet commented Feb 16, 2017

This post tell me:
Keyof is a way to enumerate javascript object prototype members but even if enums are object instances, you will not be able to enumerate them that way. Instead of that, has you expected, it will returns the prototype of number, because its making more sense than getting prototype of Object.

@styfle
Copy link
Contributor

styfle commented Dec 15, 2017

@DanielRosenwasser Your answer was very helpful!

However, I get an error using a const enum declaration with your solution.

Is there a workaround for this problem or do I need to submit a feature request?

@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
Question An issue which isn't directly actionable in code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants