facebook / facebook/flow

Strange behaviour when variable is inited with null

Offen
#3,802 3 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Rust
Sterne
22.3k
Forks
1.9k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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?

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.