简体   繁体   English

元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于在 React Typescript 中索引类型

[英]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.我在我的 React-native 项目中使用 Typescript。代码按照需要正常工作 output。但是得到 Z1E35BCBD085DBA1089CCFBD7085DBA 冲突。54

Sharing my code snippets分享我的代码片段

Importing json object from other config files.从其他配置文件导入 json object。

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.我想制作动态 baseURl,这就是为什么使用语言 const 来设置它的值。
Following error I am facing.我面临以下错误。

在此处输入图像描述

you need to put type on the getStorageItem function您需要在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';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React typescript - 元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引类型 - React typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 React Typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript 带有 React > Element 的 Typescript 隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 - Typescript with React > Element implicitly has an 'any' type because expression of type 'string' can't be used to index 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 - TypeScript 错误 - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type - TypeScript Error Typescript 错误:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 - Typescript error : Element implicitly has an 'any' type because expression of type 'string' can't be used to index TypeScript 错误:“元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 - TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type TypeScript 错误元素隐式具有“任何”类型,因为“任何”类型的表达式不能用于索引类型 - TypeScript error Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 反应 typescript 错误 - 元素隐式具有任何类型,因为类型字符串的表达式不能用于索引类型 {} - react typescript error - element implicitly has an any type because expression of type string cant be used to index type {} TypeScript - 元素隐式具有“any”类型,因为“storyFile”类型的表达式不能用于索引类型“{}” - TypeScript - Element implicitly has an 'any' type because expression of type '“storyFile”' can't be used to index type '{}' 元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 '{}' - React Anagram - Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}' - React Anagram
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM