Flowtype:不安全的实例变量访问

Flowtype: Unsafe instance variable access

/* @flow */

class Foo {
    blah: string;
    bar: string;
    constructor(blah: string, bar?: string) {
            this.blah = blah
            if (bar) {
                this.bar = bar
            }
    }
}

const foo = new Foo('okok')
foo.bar

https://flowtype.org/try/#0PQKgBAAgZgNg9gdzCYAoVBjGBDAzrsAMTjjAG9UBIAI2wCcAuMXAFzoEsA7AcwG5UAvugxxOrMFBJgAvGE4BTJMTgAKAJSpJcAHS06QA

这不会引发流类型错误吗?

编辑:

这可能回答了我的问题...

https://flowtype.org/docs/nullable-types.html#undefined-values-and-optional-types

Undefined Values and Optional Types Undefined values, just like null, can cause issues too. Unfortunately, undefined values are ubiquitous in JavaScript and it is hard to avoid them without severely affecting the usability of the language. For example, arrays can have holes for elements; object properties can be dynamically added and removed. Flow ignores the possibility of undefined resulting from object property and array element accesses. Being stricter would force the programmer to do undefined checks (like null checks) on each dereference of an array element or object property to get anything useful done.

However, Flow does detect undefined local variables and return values, and it considers optional parameters and properties to possibly be undefined. As such, uses of these types must be guarded by undefined checks to avoid errors.

目前 (v0.36) class Flow

不保证字段
class Foo {
  bar: string;
}

new Foo().bar // no error

0.47 开始,flow 似乎正确地捕获了这些错误(并且可能对于某些早期版本)。

请注意,如果您扩展一个 class 流不理解的,它不会捕获这些错误。

// @flow
import Model from './Model'; // a non-flown file
class A extends Model {}
const a = new A();
a.bar; // will not fail, because Model might have a bar field.