-1

I want to use this generic helper function to create form data:

function createFormData(data: any): FormData {
  const formData = new FormData();

  for (const key in data) {
    formData.append(key, data[key]);
  }

  return formData;
}

But the ESLint gives me an unexpected any warning. When I change the parameter type any to object it shows me the following error message for data[key]:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'. No index signature with a parameter of type 'string' was found on type '{}'

How can I fix my code here in order that no error messages or warnings are shown (without just turning off the linter)?

winklerrr
  • 13,026
  • 8
  • 71
  • 88
  • 1
    Duplicate of [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) – Guy Incognito Oct 11 '20 at 15:55
  • @GuyIncognito I don't think it's a duplicate. I'm asking explicitly for function parameter handling and how to surpress the ESLint warning for the usage of any – winklerrr Oct 11 '20 at 17:23

1 Answers1

0

Instead of any you could provide the value data type also

function createFormData(data: {[key: string]: any}): FormData {
  const formData = new FormData();

  for (const key in data) {
    formData.append(key, data[key]);
  }

  return formData;
}
Alok Takshak
  • 282
  • 1
  • 6