A
A
Alex Krynytskyi2021-11-29 23:14:56
JavaScript
Alex Krynytskyi, 2021-11-29 23:14:56

How to correctly specify the key type in TypeScript?

interface LangKey {
  header: string,
  content: string
}

interface Lang {
  en: LangKey,
  ru: LangKey,
}


const translation: Lang = {
  en: {
    'header': 'Header content',
    "content": 'Content'
  },
  
  ru: {
    'header': 'Хедер контент',
    "content": 'Контент'
  }
}

export const getTranslation = (text: string, lang:string) => {
  return translation[lang][text];
}


return translation[lang][text];

gives this error Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Lang'.

Tell me how to deal with it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2021-11-30
@bingo347

https://www.typescriptlang.org/docs/handbook/2/key...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question