facebook / facebook/flow

Strange behaviour when variable is inited with null

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

Description

Hi!

I've got a test, that looks like this

```js
declare function it(name: string, fn: any): void;
declare function describe(fn: any): void;
declare function beforeEach(fn: any): void;

declare function f(): {prop: string};

describe(() => {
let result = null;

beforeEach(() => {
result = f();
});

it('some test', () => {
result.prop;
});
});
```

I've got some function `f()` that always returns some value that is not a null, but I init `result` variable with null value. Flow shows me an error on this test:

```
15: result.prop;
^ property `prop`. Property cannot be accessed on possibly null value
15: result.prop;
^ null
```

The error disappears if I change `let result = null;` to `let result;`. ([Here](https://flow.org/try/#0CYUwxgNghgTiAEAzArgOzAFwJYHtXywwApUoBbEALngGcMYtUBzAGiVWqlQE8BKagG44swANwAoUJFgIU6bHnigaYBgCMQRRB3hc+g4WMnhocJGky58GxDjgBRKGAAWWnXv7whIicelm5S0VEIk8AbwAHGBwI6joGZgBfX2VVLA0iUPgAXgA+eDDxeHgIEAx4OBpkCHLs+FRqiAliovgbOxBHF0zeHPzC4uLK6tqkUOb4RN4J1sIiAHIaHAp4DBA6ebYsvILWofWRgDoomImpiXOgA) is an example in Flow REPL)

Is it an expected behaviour or a bug?

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.