facebook / facebook/flow

Child properties do not properly influence parent object's type

未关闭
#1,956 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看
feature request Typing: refinements
主要语言
Rust
星标
22.3k
派生
1.9k
PR 合并指标
30 天内没有已合并 PR

描述

If you alias an object's field (or simply deconstruct it) and write a guard statement that specifies a property of the obtained value, original object's type does not reflect the newly discovered properties.

Consider the following:

``` js
type Foo = {a: '1', b: any} | {a: '2'}

function test(foo: Foo) {
const { a } = foo // deconstruction
switch (a) {
case '1':
// at this point Flow knows what `a` is but not what `foo.a` is
console.log(foo.b) // this fails
break
}
if (a === '1') {
// at this point Flow knows what `a` is but not what `foo.a` is
console.log(foo.b) // this fails
}
}
```

However this works as expected:

``` js
type Foo = {a: '1', b: any} | {a: '2'}

function test(foo: Foo) {
switch (foo.a) {
case '1':
console.log(foo.b) // this works
break
}
if (foo.a === '1') {
console.log(foo.b) // this works
}
}
```

Seeing that `a` was not mutated between the calls, its type properties should also affect its parent's properties within the scope of the guard (`if`, `switch/case` etc.).

Currently it's super inconvenient to write code as you have to work around this by always referring to the parent object.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。