流 - 静态类型检查失败

时间:2018-01-21 07:11:17

标签: javascript flowtype

我来自TypeScript的世界,所以我有点困惑为什么以下不会失败。



class Bar {
  foo: string
}


let bar = new Bar();
let test = bar.foo;
test = 4; // all is good here, while it should fail!




2 个答案:

答案 0 :(得分:0)

它被称为类属性语法https://flow.org/en/docs/types/classes/

在你的例子中

class Bar {
  foo: string // not defined
}


let bar = new Bar()
let test = bar.foo // Copy value not type, should use let test: string = bar.foo
test = 4

答案 1 :(得分:0)

您的代码不会失败,因为您基本上已将变量application.properties定义为test。分配值时,不会在变量之间传递类型。

如果要将变量any强制为类型test,则应定义其类型。

string

这会抛出以下错误

let test: string = bar.foo; // ok
test = 4; // flow error
相关问题