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

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

I would like to put files into file type input and then in the class fetch and set. The getter looks like this and it works, but I have a problem with the setter because there is an error: "Property ‘item’ is missing in type ‘File []’ but required in type ‘FileList’". I created an auxiliary interejs to retrieve specific properties.

interface IFile{
url: string;
name: string;
}
class File{
    [x: string]: any;
   
    get files(): File[] {
        const input = (this.querySelector('[name="Input"]') as HTMLInputElement).files;
        return input as any;
    }

    
    set files(value: File[]) {
        (this.querySelector('[name="Input"]') as HTMLInputElement).files = value;
    }
}

>Solution :

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

  1. You cannot name your class File. Since File is an interface defined by TypeScript. (Not sure if you are trying to override the native type, which is something I wouldn’t recommend). You should change File to something else, eg. InputFile, which would already be helpful.

  2. In the line:

const input = (this.querySelector('[name="Input"]') as HTMLInputElement).files;

The type of input variable is FileList.

  • FileList is a superset of the type File.

  • Hence, you can assign FileList type to File in your getter.

  • However, in your setter, you cannot assign a File[] to FileList.

Changing the return type of your getter to FileList and making corresponding changes in your setter should fix the problem.

Ultimately, your class could look something like:

class MyFile {
    [x: string]: any;
   
    get files(): FileList {
        const input = (this.querySelector('[name="Input"]') as HTMLInputElement).files;

        if(input === null) {
          // handle null
          throw Error();
        }

        return input;
    }

    
    set files(value: FileList) {
        (this.querySelector('[name="Input"]') as HTMLInputElement).files = value;
    }
}

I hope this helps!

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