Skip to content

Cannot use the enum[member] notation in types #46473

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
pushkine opened this issue Oct 21, 2021 · 7 comments
Closed

Cannot use the enum[member] notation in types #46473

pushkine opened this issue Oct 21, 2021 · 7 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@pushkine
Copy link
Contributor

Bug Report

🕗 Version & Regression Information

ts stable & nightly

⏯ Playground Link

Playground Link

💻 Code

const enum CharCode {
    "(" = 40,
    ")" = 41,
    "a" = 97,
    "b" = 98,
    "c" = 99,
}

const lowerA = CharCode.a;
type LowerA = CharCode.a;

const parensLeft = CharCode["("];
type ParensLeft = CharCode["("];
//                         ^^^ Error: Property '(' does not exist on type 'CharCode'. (2339)

type ParensLeft2 = typeof CharCode["("]; // No Error

🙁 Actual behavior

The enum[member] notation does not work in types, rendering members that are not valid identifiers inaccessible.
Using typeof is required to make it work.

🙂 Expected behavior

The enum[member] notation works in types.

@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 21, 2021
@DanielRosenwasser
Copy link
Member

This is working as intended - that syntax is an indexed access type. If you want to grab the type of "(", you need to write (typeof CharCode)["("].

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Oct 21, 2021

Also, to be explicit, typeof CharCode["("] is (typeof CharCode)["("] and not typeof (CharCode["("]). That's why that last alias works.

#19707 discusses this further.

@pushkine
Copy link
Contributor Author

pushkine commented Oct 21, 2021

This is working as intended

I don't understand how that is intended, or why typeof is necessary. Using const enum means CharCode is not an object in runtime, referring to it using typeof as if it were a runtime object makes no sense.

@fatcerberus
Copy link

Because CharCode is the type of a value of the enum, a number, and number["("] doesn’t make sense. So if typeof didn’t work, you couldn’t do this at all.

Also, const enum can have runtime representation in certain cases, IIRC.

@pushkine
Copy link
Contributor Author

@fatcerberus I'm not sure what you're trying to say, directly accessing properties enum.member always works fine without typeof both as a value and as a type

Please explain why CharCode.a means number but CharCode["a"] means number["a"]

@fatcerberus
Copy link

fatcerberus commented Oct 22, 2021

let x: CharCode = CharCode.a;
console.log(x["("]);  // type error

You now have a number value stored in x. Foo["bar"], as a type, means that given a value of type Foo, what type do I get if I index that value with ["bar"]. If Foo is number or an enum type, then x.bar makes no sense.

As for your last question: CharCode.a happens in value space. CharCode["a"] in your example happens in type space. That's the difference.

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' 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
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants