Igor Zvyagin
Igor Zvyagin

Reputation: 474

Typescript. How to get enum item data as a type

I have an enum type, for example Currency. I cant to change it because it automatically builds on a graphql schema. I want to use it for my data, but I dont know how to do.

enum Currency {
  rub = 'RUB',
  usd = 'USD',
  eur = 'EUR',
}

const data: { currency: Currency[keyof typeof Currency] } = {
  currency: 'RUB',
};

Errors

TS2339: Property 'eur' does not exist on type 'Currency'.
TS2339: Property 'rub' does not exist on type 'Currency'.
TS2339: Property 'usd' does not exist on type 'Currency'.

Upvotes: 0

Views: 178

Answers (1)

David Gomes
David Gomes

Reputation: 5835

I'm not sure it's possible to do exactly what you're trying to do but most likely, you'd want to do something like this:

const data: { currency: Currency } = {
  currency: Currency.rub,
};

This is how enums are typically used (by always explicitly referencing the enum when getting one of its values).

Upvotes: 1

Related Questions