[英]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
:您可能想尝试为您的状态basicDetails
和errorMessages
创建一个接口:
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.