Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter FileList using TS

I would like to remove the respective image from the array after clicking on a button. If I declare the whole thing as follows, I get the below message or error from TypeScript

const [fileList, setFileList] = useState<FileList | null>(null);

const deleteImage = (image: File) => {
const filteredList: FileList = Array.from(fileList!!).filter(function (img) {
  return img !== image;
});

setFileList(filteredList);

};

Property ‘item’ is missing in type ‘File[]’ but required in type ‘FileList’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The Mozilla reference page for FileList notes that this interface has essentially been deprecated, and recommends that you just use an array of File objects instead.

That’s what you’re doing anyway, ignoring the type declarations – Array#filter returns an array, not a file list. So, you could just change FileList to File[] throughout.

If you really need to preserve compatibility with FileList (for an external API perhaps?) then you’ll need to wrap that array in something that implements the .item(..) accessor. It does not appear that a FileList constructor is available in JavaScript.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading