facebook / facebook/flow

Child properties do not properly influence parent object's type

Open
#1,956 0 comments 1 reaction 0 assignees View on GitHub
feature request Typing: refinements
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.