Strange behaviour when variable is inited with null
Hi!
I've got a test, that looks like this
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 is an example in Flow REPL)
Is it an expected behaviour or a bug?
This is expected. Flow has no way of knowing that beforeEach callback is going to be executed before it callback, so result has type null | {prop: string}
Why does removing = null makes Flow to behave in other way? Isn't it equal undefined | {prop: string}?
@verkholantsev this is either a bug or (most likely) an unsafe feature to support behavior that you want