简体   繁体   中英

New “Missing annotation” error in flowjs 0.54.0

After switching to flow 0.54.0 the following code fragment:

function runKarmaTest() {
    const KARMA_CONFIG = {};
    return new Promise(function (resolve, reject) {
        new karma.Server(KARMA_CONFIG, function (exitCode) {
            if (exitCode === 0) {
                resolve();
            } else {
                reject(exitCode);
            }
        }).start();
    });
}

reports the following error:

Error: scripts/runKarma.js:76
               v----------------------------------------
 76:    return new Promise(function (resolve, reject) {
 77:            new karma.Server(KARMA_CONFIG, function (exitCode) {
 78:                    if (exitCode === 0) {
...:
 84:    });
        -^ type parameter `R` of constructor call. Missing annotation

in the line return new Promise(function (resolve, reject) { and I cannot seem to figure out what's wrong ?

It looks like the thing it wants to know is the type of value wrapped by the promise. In this case it looks like it's just undefined , since the success case doesn't give any value. You can probably annotate the function that returns this as returning a Promise<void> or something like that to make this error go away.

It is curious that this happens in 0.54 and not before, though.

The Promise<void> annotation in the runKarmaTest function solves this problem:

function runKarmaTest(): Promise<void> {
    const KARMA_CONFIG = {};
    return new Promise(function (resolve, reject) {
        new karma.Server(KARMA_CONFIG, function (exitCode) {
            if (exitCode === 0) {
                resolve();
            } else {
                reject(exitCode);
            }
        }).start();
    });
}

I'm still not sure:

  • why this annotation is needed in 0.54 and not before
  • why the flow Type Inference cannot deduce it from the missing parameter in resolve

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM