简体   繁体   English

元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 - TypeScript 错误

[英]Element implicitly has an 'any' type because expression of type 'string' can't be used to index type - TypeScript Error

  const [basicDetails, setBasicDetails] = useState({
    companyName: "",
    pocName: "",
    pocEmail: "",
    pocMobile: "",
    businessCard: { file: "", type: "", url: "" },
    companyBaseAddress: [defaultAddress]
  });

  const [errorMessage, setErrorMessage] = useState({
    companyName: "",
    pocName: "",
    pocEmail: "",
    pocMobile: "",
    businessCard: { file: "", type: "", url: "" },
    companyBaseAddress: [defaultAddress]
  });

const validation = () => {
    Object.keys(errorMessage).map((field => basicDetails[field] === "" ?
      setErrorMessage(prevState => ({ ...prevState, [field]: "Can not be Empty" })) : null));
  };

Error is -> Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ companyName: string;错误是 -> 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{ companyName: string; pocName: string; pocName:字符串; pocEmail: string; pocEmail:字符串; pocMobile: string; pocMobile:字符串; businessCard: { file: string;名片:{文件:字符串; type: string;类型:字符串; url: string; url:字符串; }; }; companyBaseAddress: { line1: string; companyBaseAddress:{ line1:字符串; line2: string;第2行:字符串; city: string;城市:字符串; region: string;区域:字符串; zip_code: string;邮编:字符串; country: string;国家:字符串; }[]; }[]; }'. }'。 No index signature with a parameter of type 'string' was found on type '{ companyName: string;在“{ companyName: string; 类型”上找不到带有“string”类型参数的索引签名; pocName: string; pocName:字符串; pocEmail: string; pocEmail:字符串; pocMobile: string; pocMobile:字符串; businessCard: { file: string;名片:{文件:字符串; type: string;类型:字符串; url: string; url:字符串; }; }; companyBaseAddress: { line1: string; companyBaseAddress:{ line1:字符串; line2: string;第2行:字符串; city: string;城市:字符串; region: string;区域:字符串; zip_code: string;邮编:字符串; country: string;国家:字符串; }[]; }[]; }' }'

I had this issue in the past as well.我过去也有这个问题。 You may want to try to create an interface for both your states basicDetails and errorMessages :您可能想尝试为您的状态basicDetailserrorMessages创建一个接口:

interface BasicDetailsInterface {
  [key: string]: string,
  companyName: string,
  pocName: string,
  pocEmail: string,
  // continue for other props
}

interface ErrorMessageInterface {
  [key: string]: string,
  companyName: string,
  pocName: string,
  pocEmail: string,
  // continue for other props
}

And then update the declaration of your states to reflect the interfaces you just created:然后更新您的状态声明以反映您刚刚创建的接口:

const [basicDetails, setBasicDetails] = useState<BasicDetailsInterface>({

const [errorMessage, setErrorMessage] = useState<ErrorMessageInterface>({

暂无
暂无

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

相关问题 TypeScript 错误元素隐式具有“任何”类型,因为“任何”类型的表达式不能用于索引类型 - TypeScript error Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 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 in React Typescript 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 React Typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript map() 中的打字稿错误:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 - Typescript error in map(): Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 带有 React &gt; Element 的 Typescript 隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 - Typescript with React > 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 - 元素隐式具有“any”类型,因为“storyFile”类型的表达式不能用于索引类型“{}” - TypeScript - Element implicitly has an 'any' type because expression of type '“storyFile”' can't be used to index type '{}' 元素隐式具有“任何”类型,因为“任何”类型的表达式不能用于索引类型“{服务:字符串; } - Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ service: string; } 元素隐式具有 &#39;any&#39; 类型,因为类型 &#39;string&#39; 的表达式不能用于索引类型 &#39;{...}&#39; - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{...}'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM