facebook / facebook/flow

Unexpected error on object intersection

Open
#3,148 0 comments 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

Hey! This may be a misunderstanding of how intersection types work [in Flow] but to me I'd expect this to work. Hopefully there's a logical alternative or it may just be a bug! :)

Flow version is latest (v0.37.2 I believe).

Application code:

```
import {Record} from 'immutable';
const ARecord = Record({a: undefined});

type A = ImmRecord<{a: string}>;
type B = ImmRecord<{a: '2'}>;

const b: B = new ARecord({a: '2'});
const a: A = b;
```

`ImmRecord` type declaration:

```
import type {Map, Seq} from 'immutable';

declare type ImmRecord = {
withMutations: (u: Function) => ImmRecord,
merge: (v: $Shape) => any,
get: (k: string, d?: any) => any,
set: (k: string, v: any) => ImmRecord,
setIn: (k: Array, v: any) => ImmRecord,
update: (k: string, u: (any) => any) => ImmRecord,
updateIn: (k: Array, u: (any) => any) => ImmRecord,
entrySeq: () => Seq,
} & T;

```

And the errors I see:

```
55: const a: A = b;
^ intersection. This type is incompatible with
9: declare type ImmRecord = {
^ object type. See lib: flow/immutable.js.flow:9
Member 1:
9: declare type ImmRecord = {
^ object type. See lib: flow/immutable.js.flow:9
Error:
51: type A = ImmRecord<{a: string}>;
^^^^^^ string. Expected string literal `2`
52: type B = ImmRecord<{a: '2'}>;
^^^ string literal `2`
Member 2:
52: type B = ImmRecord<{a: '2'}>;
^^^^^^^^ object type
Error:
9: declare type ImmRecord = {
^ property `merge`. Property not found in. See lib: flow/immutable.js.flow:9
52: type B = ImmRecord<{a: '2'}>;
^^^^^^^^ object type

// next error...

55: const a: A = b;
^ intersection. This type is incompatible with
51: type A = ImmRecord<{a: string}>;
^^^^^^^^^^^ object type
Member 1:
9: declare type ImmRecord = {
^ object type. See lib: flow/immutable.js.flow:9
Error:
51: type A = ImmRecord<{a: string}>;
^^^^^^^^^^^ property `a`. Property not found in
9: declare type ImmRecord = {
^ object type. See lib: flow/immutable.js.flow:9
Member 2:
52: type B = ImmRecord<{a: '2'}>;
^^^^^^^^ object type
Error:
51: type A = ImmRecord<{a: string}>;
^^^^^^ string. Expected string literal `2`
52: type B = ImmRecord<{a: '2'}>;
^^^ string literal `2`
```

Any ideas?

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.