facebook / facebook/flow

Allow refinement of exact object union types with typeof

Aperta
#4,639 6 commenti 15 reazioni 0 assegnatari Vedi su GitHub
Typing: refinements Typing: unions/intersections
Lingua principale
Rust
Stelle
22.3k
Fork
1.9k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Refining exact object union types by shape is currently VERY limited. We should be able to refine such types by their shape using `typeof` at the very least, and ideally any other common refinement techniques (`Array.isArray`, etc).

From my perspective [this should work](https://flow.org/try/#0PTAEAEDMBsHsHcBQiAuBPADgU1AMVrKALygDeAPpAQFygDOKATgJYB2A5uQL4DcqmOAEIBDRsTLkARqNoMWHbn3TZQAZVgBbLAFVWzWK3H5C5UCMZ9EkAK6sAxin2H2WFMYDyjAHLXo0UAAU1rTqWrpOAJS0APzGZIgAkIyu1oyGyliwkKDWAHRUhERFoADktgAmWJBsWOUloNGgrL7+tNZ8XIhAA):

```ts
// @flow

type Foo = {|foo: string|};
type Bar = {|bar: string|};
type SomeUnion = Foo | Bar;

function getFooOrNull (u: SomeUnion): ?Foo {
return typeof u.foo === 'undefined' ? null : u;
}
```

but fails with:

```
4: type Bar = {|bar: string|};
^ property `bar`. Property not found in
7: function getFooOrNull (u: SomeUnion): ?Foo {
^ object type
7: function getFooOrNull (u: SomeUnion): ?Foo {
^ property `foo`. Property not found in
4: type Bar = {|bar: string|};
^ object type
```

---

This limitation is also described in [this comment](https://github.com/facebook/flow/issues/3614#issuecomment-290622704), and is related to #4328.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.