flow icon indicating copy to clipboard operation
flow copied to clipboard

Optional values reporting error after a continue in a loop

Open Xesued opened this issue 10 years ago • 3 comments

/* @flow */
type OptionalValues = {
    optional?: {value: string};
}
declare var someValues: OptionalValues[];

for( const ov of someValues) {
    if (!ov.optional){
        continue;
    }

    ov.optional.value = 'test';   // Error: property `value`. Property cannot be assigned on possibly undefined value
}

Seems to be very similar to #1167 and #935

Xesued avatar May 26 '16 16:05 Xesued

@Xesued thanks for the repro - looks like a bug that's specific to continue and property refinements (the latter is what distinguishes it from #1167).

Until fixed, a workaround is to refactor the control flow as follows (if feasible):

for( const ov of someValues) {
   if (ov.optional) {
     ov.optional.value = 'test';  // no error
     break;
  }
}

bhosmer avatar Jun 01 '16 00:06 bhosmer

Adding to this, break also exhibits the same bug inside of for loops -- replacing continue with break in the provided repro shows it.

@bhosmer: Mind if I take a look at this one? I feel like writing some OCaml.

ngavalas avatar Jun 06 '16 02:06 ngavalas

This is now fixed.

SamChou19815 avatar Oct 04 '22 20:10 SamChou19815