4

I'm getting this error when I try to access a value inside the Enum Localization with the variable locale that is a string.

enum Localization {
    'en-US' = '.com',
    'pt-BR' = '.com.br',
    'en-CA' = '.com.ca',
    'en-AU' = '.com.au',
    'en-IE' = '.com.ie',
    'string' = 'string'
};
 const locale:string = 'pt-BR' //This value will come from DB.
 const result = Localization[locale];

Error: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof Localization'. No index signature with a parameter of type 'string' was found on type 'typeof Localization'.

error index typescript

In Javascript works normally.

     const Localization = {
       'en-US': '.com',
       'pt-BR': '.com.br',
       'en-CA': '.com.ca',
       'en-AU': '.com.au',
       'en-IE': '.com.ie',
     };
     
const locale = 'pt-BR';

console.log(Localization[locale]); // returns ".com.br"

I would like to know:

1 - How to convert the code Javascript to work in TypeScript?
2 - Why typescript is returning this error?
3 - If possible, I would like some reference links to read and understand why this error on TypeScript.
4 - What is the better approach to access data inside objects in TypeScript?

Thank you so much.

4
  • 1
    I ran your example on a few online ts editors, but they all ran okay. Can you share your config file? Commented Sep 1, 2020 at 21:08
  • It's working with typescript: stackblitz.com/edit/typescript-39eczb?file=index.ts Commented Sep 1, 2020 at 21:08
  • stackoverflow.com/questions/50417254/… Commented Sep 1, 2020 at 21:13
  • Hey guys, thank you so much for helping me, to get the error you need to add a type to the locale variable like const locale: string = 'pt-BR' I updated the example. Please try to run now. Commented Sep 2, 2020 at 8:24

2 Answers 2

7

The below works at least with a map type, I see no reason why it wouldnt work with an explict enum type. const result = Localization[locale as keyof typeof Localization];

Source reddit

Sign up to request clarification or add additional context in comments.

Comments

2

For something like this, I'd recommend making a key type for ease of use and to ensure type checking continues to work down the chain.

enum Localization {
  'en-US' = '.com',
  'pt-BR' = '.com.br',
  'en-CA' = '.com.ca',
  'en-AU' = '.com.au',
  'en-IE' = '.com.ie',
  'string' = 'string'
};

type LocalizationKey = keyof typeof Localization;
const locale:LocalizationKey = 'pt-BR' //This value will come from DB.
const result = Localization[locale];


// TYPECHECKING TEST:
function localization(r: Localization) {
  console.log(r)
}

localization(result);

// @ts-expect-error this line should error because it's not a key of Localization
localization('fail');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.