In my TypeScript code, I have a variable called lang
declared as a string type value, and a variable called direction
declared as an object with two elements. I also have a function that is supposed to return the value of the direction
object based on the value of lang
. However, I am encountering an error when trying to access direction[lang]
, which says "null' cannot be used as an index type." I am certain that the lang
will never be null. Can anyone help me fix this issue?
export const lang = localStorage.getItem('lang') ? localStorage.getItem('lang') : "en";
export const direction = {
ru: "rtl",
en: "ltr"
}
function getDirection() {
return direction[lang];
}