流:传入的对象与相交类型不匹配

时间:2018-11-07 22:38:40

标签: javascript flowtype

我定义了以下类型:

type FooFields = {
  foo: string,
};

type BarFields = {
  bar: string,
};

type FormFieldType = {
  type: 'foo' | 'bar',
};

type FormFields = $Shape<FooFields & BarFields> & FormFieldType;

具有以下功能来检查表单字段状态上的验证问题:

const isValid = (formFields: FormFields): boolean => {
  switch (formFields.type) {
    case 'foo':
      return isFooValid(formFields);
    case 'bar':
      return isBarValid(formFields);
  }

  return false;
};

const isFooValid = (formFields: FooFields): boolean => {
  return formFields.foo === 'hello';
};

const isBarValid = (formFields: BarFields): boolean => {
  return formFields.bar === 'hey';
};

当我尝试在下面运行以下代码...

const state = {
  bar: 'hey',
  type: 'bar',
};

isValid(state);

我从Flow收到此错误:

 isValid(state);
        ^ Cannot call `isValid` with `state` bound to `formFields` because in property `type`: Either property `type` is missing in `FooFields` [1]. Or property `type` is missing in `BarFields` [2].

References:

13: type FormFields = $Shape<FooFields & BarFields> & FormFieldType;
                             ^ [1]

13: type FormFields = $Shape<FooFields & BarFields> & FormFieldType;
                                         ^ [2]

为什么Flow应该在type中找到字段FooFieldsBarFields时尝试在FormFieldType中找到字段?

0 个答案:

没有答案