我定义了以下类型:
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
中找到字段FooFields
或BarFields
时尝试在FormFieldType
中找到字段?