1
  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; pocName: string; pocEmail: string; pocMobile: string; businessCard: { file: string; type: string; url: string; }; companyBaseAddress: { line1: string; line2: string; city: string; region: string; zip_code: string; country: string; }[]; }'. No index signature with a parameter of type 'string' was found on type '{ companyName: string; pocName: string; pocEmail: string; pocMobile: string; businessCard: { file: string; type: string; url: string; }; companyBaseAddress: { line1: string; line2: string; city: string; region: string; zip_code: string; country: string; }[]; }'

Shiva Giri
  • 105
  • 1
  • 6
  • 1
    Does this answer your question? [Element implicitly has an 'any' type because expression of type 'string' can't be used to index](https://stackoverflow.com/questions/57086672/element-implicitly-has-an-any-type-because-expression-of-type-string-cant-b) – Robby Cornelissen Sep 14 '22 at 04:53

1 Answers1

0

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:

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>({
FSperoni
  • 66
  • 4