0

I'm using typescript 3.7 Given

enum InformationSection {
  Education = 'Education label',
  Resume = 'Resume label'
}

the code

InformationSection["Education"]

returns 'Education label' and it's a valid statement but if I write

Object.keys(InformationSection).map(x => InformationSection[x])

then I get the following error:

Property 'Education label' does not exist on type 'typeof InformationSection'

what's wrong here?

Pato Loco
  • 1,205
  • 1
  • 13
  • 29
  • 1
    Does this answer your question? [Is there a simple way to get a list of keys/numbers from enum in typescript?](https://stackoverflow.com/questions/48413651/is-there-a-simple-way-to-get-a-list-of-keys-numbers-from-enum-in-typescript) – Gonzalo.- Dec 13 '19 at 18:06
  • It is not the same scenario – Pato Loco Dec 13 '19 at 18:14
  • What, specifically, is not the same scenario? The `Object.keys(...)...` line [results in a different error from the one you mention](http://www.typescriptlang.org/play/#code/KYOwrgtgBAkiBmB7AThAhgFwJaJAZWAGNtcoBvAWACgpaoBRAEzEMxxCgF4oByJltqQA2aAEbAhPADTU6UAErAAzpGBdeilRDUjxkqNQC+1APKiAVkQwA6ANbAAnkoAUcJKkH4r7AJTX0AA7OAB5cAHywCCjoJF7E7ADawQC6PgDcBlQA9FlQwMjIKFJQIIhQWCCMwKFKWADmIJhgyGoAtO0dnREAfr19-QOD1NRAA) and produces an array, not a reverse map. – jcalz Dec 13 '19 at 18:21
  • Possible duplicate of [TypeScript: Object.keys return string\[\]](https://stackoverflow.com/questions/52856496/typescript-object-keys-return-string) – jcalz Dec 13 '19 at 18:23
  • @jcalz that is for an object, not an enum – Pato Loco Dec 13 '19 at 18:39
  • The value `InformationSection` is an enum object, so... it is an object. (There is also a *type* named `InformationSection`, which is [different](https://stackoverflow.com/a/50396312/2887218).) If you write `(Object.keys(InformationSection) as Array).map(k => InformationSection[k])` it will compile (as mentioned in the [answer to the linked question](https://stackoverflow.com/a/52856805/2887218)), and it will evaluate to `InformationSection[]`, which might be what you want, although a true [mcve] would be helpful for me to be sure. – jcalz Dec 13 '19 at 18:46
  • @jcalz if that's the best possible solution I'd rather cast it as any. After all it's part of the same expression – Pato Loco Dec 13 '19 at 18:54
  • Asserting `obj` to `any` is tantamount to completely turning off the type checking, whereas asserting `Object.keys(obj)` to `Array` will still prevent you from doing crazy things like `Object.keys(obj).map(k => (obj as any) + 17)`. It's up to you, but that's the issue and its solution. Unless you have some reason why the [linked question](https://stackoverflow.com/questions/52856496/typescript-object-keys-return-string) doesn't apply here, I will close this as a duplicate. – jcalz Dec 13 '19 at 19:09

2 Answers2

0

The only way that I found is to trick typescript to make it stop complain about the invalid index type

(InformationSection as any)[x]
Pato Loco
  • 1,205
  • 1
  • 13
  • 29
0
Object.keys(InformationSection).map(x => InformationSection[x keyof typeof InformationSection])

String enums are tricky as they cannot be reverse mapped easily: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-4.html#string-enums

Source: https://stackoverflow.com/a/66683328

treatycity
  • 133
  • 1
  • 6