0

I have this object:

export const myObj = {
 a: 'someval',
 b: 'someotherval',
 c: 'asdasdasd'
};

Which then I use like:

const myVar:string = myObj[someOtherVar];

What's the proper way of doing this with Typescript?

nick
  • 2,819
  • 5
  • 33
  • 69
  • Does this answer your question? [Enforcing the type of the indexed members of a Typescript object?](https://stackoverflow.com/questions/13315131/enforcing-the-type-of-the-indexed-members-of-a-typescript-object) – Tobias S. Aug 07 '22 at 18:48
  • The proper way is for `someOtherVar` to be of type `keyof typeof myObj` or `"a" | "b" | "c"`; but you haven't defined/declared `someOtherVar` so I don't know if that works for you. Please make this code a [mre] and I could maybe answer. – jcalz Aug 07 '22 at 19:26
  • someOtherVar comes from a request, in the body (this is in an Nextjs formHandler call) so I don't know the type (although I make sure from the UI that it's either a b or c) – nick Aug 08 '22 at 13:15

0 Answers0