Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 9
    So we can use typecast: let s = "Green"; let typedColor = <keyof typeof Color> s; Commented Jun 8, 2017 at 13:06
  • Yep, and replacing let with const will work without casting. Updated example to clarify this. Thanks @SergeyT Commented Jun 8, 2017 at 20:47
  • 4
    typedColorString = Color["Black"]; now returns error TS7015: Element implicitly has an 'any' type because index expression is not of type 'number' Commented Sep 9, 2019 at 17:27
  • 37
    A one line answer: const color: Color = Color[colorString as keyof typeof Color]; Commented Nov 6, 2019 at 19:06
  • @Dominik You need a string enum Commented Feb 21, 2023 at 9:03