facebook / facebook/flow

Do...while types not treated same as while types

Open
#3,810 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

I think there's an issue dealing with the do statement here:

```js
const getSomeParentNode = (element: Element) => {
let elementParent = element.parentNode;

if (!elementParent) {
return null;
}

do {
elementParent = elementParent.parentNode;

if (!elementParent) {
return null;
}
} while (elementParent !== document.body);

return elementParent;
};
```

```
11: elementParent = elementParent.parentNode;
^ property `parentNode`. Property cannot be accessed on possibly null value
```

Whereas the regular `while` loop version correctly recalculates the type of `elementParent` at the top of the loop.

```js
const getSomeParentNode = (element: Element) => {
let elementParent = element.parentNode;

if (!elementParent) {
return null;
}

while (elementParent !== document.body) {
elementParent = elementParent.parentNode;

if (!elementParent) {
return null;
}
}

return elementParent;
};
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.