facebook / facebook/flow

Unexpected error on object intersection

未关闭
#3,148 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Rust
星标
22.3k
派生
1.9k
PR 合并指标
30 天内没有已合并 PR

描述

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?

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。