typesflowtype

Why doesn't Flow understand this simple logic?


I noticed that my project doesn't label a as undefined in this context. Could anyone help me to understand? As it seems very simple to me.

In this case a can be hard typed as a defined string without errors, even though it will obviously be undefined.

const maybeObject: ?{a: string} = null
const { a }: {a: string} = maybeObject || {}

Flow Try


Solution

  • I think this has to do with Flow's concept of unsealed objects. When you use {} you create an unsealed object. Flow does not check property reads for properties of unsealed objects that have not been assigned before. The reason above code does not error is basically because this code does not error either:

    const b = {};
    const c: string = b.a;
    

    Now why was this design chosen? I assume Flow tries to make common JavaScript patterns work without being to strict.