facebook / facebook/flow

Refinement of polymorphic type does not work

Aperta
#3,679 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Rust
Stelle
22.3k
Fork
1.9k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Refining a type which has a polymorphic subtype doesn't seem to work properly.

Here's the code:

```js
export type InnerTypeA = {
type: "A",
};

export type InnerTypeB = {
type: "B",
};

export type InnerType = InnerTypeA | InnerTypeB;

type OuterType = {
inner: T,
};

export type OuterTypeA = OuterType;
export type OuterTypeB = OuterType;

function test(a: OuterTypeB) {}
function test2(a: OuterType) {}

function b(a: OuterType) {
if (a.inner.type === "B") {
// Doesn't work:
test(a);

// Works, which means the inner refinement is still there
// test({inner: a.inner});

// Works, which means the outer refinement is also there?
// test2(a);
}
}

b({inner:{type:"B"}})
```

[Online Flow-try](https://flow.org/try/#0KYDwDg9gTgLgBDAnmYcCSA7DwoBVnACCcAvHAN4BQcCBAXHAESGMA0lAvgNyWWiSxaKdFhz4UAIVIVqQ4A0YS2nHn3DR4SYZmx4C0nWILEAPiN3jgE1VtQB5AK4wjKADy4GhvSgB80qjQAlqJQDLjs3Lz8GnJwjs7eRNLxLsCuXpaEPjzRgrZxTqlSZCmJ6SGWEtm8AGYOGADGMIEQGAjAAM4wABQAhgyllQCUFByUdY3Nre1dAEzuPn0DhWW4PiPkY+P1TS1tAEbunhUEi-0FCZYLG7KBNXB9AHTBuo-5JB9MSjc0NAD0fzgABEIJ0MAByeAAd2gAGs6LIaM4un0hqpfnAAXAAOpwjqsOBQgAWgQaRLgAFtgL0MB0EETUC8cHAoMAasFgFSMPBAnSuoEADYC+k4YCIzGA5E9chM0JwXrPEIcNHi8VY3FQWH4wkksmU6m0kVwCArFlsjlcnl03oCjoQEWsgD8aslnRgs1RPBoYy2+26MpCdHItjoikYHGVQA).

This doesn't seem to be expected "pessimistic refinement" behaviour, since no other function calls happen between the guard and the type check.

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.