Javascript 将流类型超类型的对象指定给子类型会导致错误
我的印象是,只要在类型中声明的所有属性都存在,流中不精确的对象类型就可以提供一个没有在类型中声明属性的对象 Flow中不精确的对象类型声明表示“该对象可以具有这些属性中的任何一个,以及更多未指定的属性”。因此,子类型的对象应该能够被指定为SuperType类型的对象,并且仍然有效 我错过了什么 我相信这与嵌套的对象类型有关,但是如果我不能修改genericPlan(子类型)上未声明的属性,那又有什么关系呢Javascript 将流类型超类型的对象指定给子类型会导致错误,javascript,flowtype,Javascript,Flowtype,我的印象是,只要在类型中声明的所有属性都存在,流中不精确的对象类型就可以提供一个没有在类型中声明属性的对象 Flow中不精确的对象类型声明表示“该对象可以具有这些属性中的任何一个,以及更多未指定的属性”。因此,子类型的对象应该能够被指定为SuperType类型的对象,并且仍然有效 我错过了什么 我相信这与嵌套的对象类型有关,但是如果我不能修改genericPlan(子类型)上未声明的属性,那又有什么关系呢 /* @flow */ type SuperType = { plan: {
/* @flow */
type SuperType = {
plan: {
id: string,
location: {
id: string,
team: {
id: string,
},
},
},
};
type SubType = {
plan: {
id: string,
location: {
id: string,
},
},
};
const planWithTeam: SuperType = {
plan: {
id: '',
location: {
id: '',
team: {
id: '',
},
},
},
};
// The following throws this error:
// Cannot assign planWithTeam to genericPlan because property team is
// missing in SubType [1] but exists in SuperType [2] in property plan.location.
const genericPlan: SubType = planWithTeam;
没有。要使输入协变,只需添加一个加号:
type SubType = {
+plan: {
id: string,
+location: {
id: string,
},
},
};
更改名称
计划
=>一般计划