TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Strange error message when accessing undefined property of an enum

Open Sainan opened this issue 10 months ago • 5 comments

🔎 Search Terms

enum, property, does not exist

🕗 Version & Regression Information

Tested 5.8.2

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/KYOwrgtgBAymCWAXAzlA3gKClAEgUQEEAlAFRigF4oBGDAXwwGMB7EZZgG2ADoPmBzABRwkybjAAKBACJ4YASgDcQA

💻 Code

enum Suits {
  HEARTS = 1
}
console.log(Suits.SPADES);

🙁 Actual behavior

Property 'SPADES' does not exist on type 'typeof Suits'.

🙂 Expected behavior

  • Property 'SPADES' does not exist on type 'Suits'
  • Property 'SPADES' does not exist on enum 'Suits'

Additional information about the issue

No response

Sainan avatar Mar 16 '25 11:03 Sainan

This is working as intended. typeof Suits refers to the "container object" that contains all the enum values. The error message will refer to Suits when dealing with a value of your enum, e.g.: function foo(v: Suits) { v.missing }

It's essentially the same as:

class Foo {}
Foo.bar

MartinJohns avatar Mar 16 '25 11:03 MartinJohns

Property 'SPADES' does not exist on type 'Suits'

This would be the error you'd see if you wrote

function fn(x: Suits) {
  console.log(x.SPADES);
}

RyanCavanaugh avatar Mar 17 '25 17:03 RyanCavanaugh

Ah, why do I never get email notifications when @MartinJohns writes a reply.

Also, I'm confused, I thought enums were not a native JavaScript type... wouldn't it just be a number/string/whatever the underlying type is?

Sainan avatar Mar 17 '25 17:03 Sainan

Yeah, no, this is definitely bad error reporting when compared to types, which should be more or less the same:


enum Suits {
  HEARTS = 1,
  CLUBS = "clubs"
}
function func(s: Suits): void {
  console.log(s.substring(1));
}
Property 'substring' does not exist on type 'Suits'.

type Suits = 1 | "clubs";
function func(s: Suits): void {
  console.log(s.substring(1));
}
Property 'substring' does not exist on type 'Suits'.
  Property 'substring' does not exist on type '1'.

Sainan avatar Mar 17 '25 17:03 Sainan

Also, I'm confused, I thought enums were not a native JavaScript type... wouldn't it just be a number/string/whatever the underlying type is?

Typescript does not operate on underlying native types, it operates on the types as defined in typescript.

Regarding the OP... in Suits.SPADES, Suits does not refer to the type Suits, it refers to the value Suits. The type of the value Suits is typeof Suits. There is no named type to reference, just like when we refer to the type of a unique symbol, where its type is typeof MySymbol.

For another analogy, consider class X { }. This defines an instance type X, and it defines a value X, which is a constructor and whose type is typeof X.

snarbles2 avatar Mar 17 '25 18:03 snarbles2

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

typescript-bot avatar Mar 20 '25 01:03 typescript-bot