You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Per the title: if you have an array of maybe types, and filter out null values from that array (e.g. .map(val => val != null), flow fails to detect the "unwrapping" of these values and keeps treating the array as one of maybes. As a result, totally valid and safe future operations emit errors during typechecking.
// @flow// Problem conceptletmaybes: Array<?number>=[1,2,3];letactuals=maybes.filter(val=>val!=null)// Should now be Array<number>, but stays as Array<?number>.filter(val=>val>0);// Discovered this waytypeStory={id: number};letstory_ids=[1,2,3];letstories: Array<Story>=[{id: 2},{id: 3}];letexample=story_ids.map(id=>stories.find(story=>story.id===id)).filter(story=>story!=null).filter(story=>story.id>2)// Worksletworks=story_ids.map(id=>stories.find(story=>story.id===id)).filter(story=>story!=null&&story.id>2)// all future operations require the null check as well
src/example.js:7
7: .filter(val => val > 0);
^^^ null. This type cannot be compared to
7: .filter(val => val > 0);
^ number
src/example.js:7
7: .filter(val => val > 0);
^^^ undefined. This type cannot be compared to
7: .filter(val => val > 0);
^ number
src/example.js:16
16: .filter(story => story.id > 2)
^^ property `id`. Property cannot be accessed on possibly undefined value
16: .filter(story => story.id > 2)
^^^^^ undefined
Poking around, it looks like this should work, but it's at best unclear how to get flow to pick up on this.
The text was updated successfully, but these errors were encountered:
Per the title: if you have an array of maybe types, and filter out null values from that array (e.g.
.map(val => val != null)
, flow fails to detect the "unwrapping" of these values and keeps treating the array as one of maybes. As a result, totally valid and safe future operations emit errors during typechecking.Poking around, it looks like this should work, but it's at best unclear how to get flow to pick up on this.
The text was updated successfully, but these errors were encountered: