2
votes

TS2322: Type 'File' is not assignable to type 'typeof File'. Property 'prototype' is missing in type 'File'.

I get this error. I don't know why I get it and I don't know how to solve it. If anyone would be so kind to give me a hand I would really appreciate it!

I have this public class property:

registry = File;

The getRegistryFile function:

private async getRegistryFile(): Promise<File> {
        if (this.files === undefined || this.files.length === 0) {
            return Promise.reject('No files where returned from the server.');
        }

        const registryFileName = await this.createRegistryFileName();

        this.files.forEach((element) => {
            if (element === registryFileName) {
                console.log('File registry found.');
                return Promise.resolve(element);
            }
        });
    }

And the function I was constructing:

public WorkInProgress(file: File) {
    this.getRegistryFile().then((file) => this.registry = file);
}
2

2 Answers

4
votes

I have this public class property:

registry = File;

Is this a declaration of registry property which should have File type? Then it should be

registry: File;

Or is it an initialization of a property that holds File class constructor? Then it's type is really typeof File, and it can not be used as an instance of File.

2
votes

See commend from @artem: should be registry: File not registry = File Also, you cannot return a value from Array.forEach() (you are trying to return a Promise) instead do it like this:

private async getRegistryFile(): Promise<File> {
    if (this.files === undefined || this.files.length === 0) {
        return Promise.reject('No files where returned from the server.');
    }

    const registryFileName = await this.createRegistryFileName();

    for (let element of this.files) { // replaced Array.forEach with for-of loop
        if (element === registryFileName) {
            console.log('File registry found.');
            return Promise.resolve(element);
        }
    };
    return Promise.reject(new Error("File not found"));
}