Child properties do not properly influence parent object's type
- Langage dominant
- Rust
- Étoiles
- 22.3k
- Forks
- 1.9k
- Métriques de merge des PR
- Aucune PR mergée en 30 j
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.
Guide de contribution
Ouvrir le guide de contribution
Évaluation
Cette issue n'a pas encore été évaluée.