facebook / facebook/flow

Refinement of polymorphic type does not work

Open
#3,679 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

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.

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.