Surbhi Davara
Surbhi Davara

Reputation: 251

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type in React Typescript

I am using Typescript in my React-native project.Code working properly as per desired output.but getting typescript conflicts.

Sharing my code snippets

Importing json object from other config files.

import api from 'config/api.json'

  languageSet = async (value: string) => {
  setStorageItem('language', value)
  this.setState({ setLoading: true })

  const language = await getStorageItem('language');

  if (language) {
    const baseURL = api[language][environment] ? api[language][environment].api : api[language].qa.api;
  
  }
  else {
    const baseURL = api.ch[environment] ? api.ch[environment].api : api.ch.qa.api;
  }
}

I want to make dynamic baseURl that's why using language const to set it's value.
Following error I am facing.

enter image description here

Upvotes: 1

Views: 153

Answers (1)

Yoel
Yoel

Reputation: 7985

you need to put type on the getStorageItem function

try this way

const language = await getStorageItem('language') as 'ch';

if you have some language you can do

const language = await getStorageItem('language') as 'ch' | 'other_language';

Upvotes: 1

Related Questions