2

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);
}
MaestroMaus
  • 342
  • 1
  • 6
  • 18

2 Answers2

4

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.

artem
  • 46,476
  • 8
  • 74
  • 78
2

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"));
}
DoronG
  • 2,576
  • 16
  • 22
  • I appreciate you pointing this out. But the next question I have now is: Why can't I return from a for each? That seems really arbitrary. – MaestroMaus May 17 '18 at 06:21
  • I found this: https://stackoverflow.com/questions/43555904/foreach-for-in-not-returning-values Is there a special rule I can use to know when a function allows me to return values? Or do I need search each loop mechanism and remember? – MaestroMaus May 17 '18 at 06:32
  • 1
    @MaestroMaus, you can look at the documentation of `Array.prototype.forEach` (look [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)). as you can see `.forEach` expects a callback function which does not return a value, hence, if it returns a value it ignores it. Think of the `.forEach` as: `for (let i = 0; i < array.length; i++) callback(item, i, array)`. As you can see it does nothing with the result of `callback()`. In other words RTFM ;) – DoronG May 17 '18 at 14:37