I have the following Flow Props type for a component:type Props = { // <...> update: ({ dates?: DateRange }) => void};I also have the following exported type:export type SearchContextType = { // <...> update: ({ dates?: DateRange, location?: Location }) => void};When I try to pass props to the first component using the second type I get the following error:I understand the error, but my question is: how can I get around it properly?Example 解决方案 First - we will simplify the example:type SubType = { dates?: string, location?: string };type Foo = (arg: SubType) => void;type SuperType = { dates?: string };type Bar = (arg: SuperType) => void;function convert (arg: Foo): Bar { return arg; // ^ Cannot return `arg` because property `location` is missing in `SuperType` [1] but exists in `SubType` [2] in the first argument.}In other word we just use typecasting to convert Foo into Bar:const anyObj = ({}: any);((anyObj: Foo): Bar);// ^ Cannot cast object literal to `Bar` because property `location` is missing in `SuperType` [1] but exists in `SubType` [2] in the first argument.Or we can say that we convert SuperType into SubType((anyObj: SuperType): SubType);// ^ Cannot cast `anyObj` to `SubType` because property `location` is missing in `SuperType` [1] but exists in `SubType` [2].To convert SuperType into SubType we can use $Shape:// Correct((anyObj: SuperType): $Shape<SubType>);TLDR:export type SearchContextType = { dates: DateRange, location: GoogleMapPosition, update: ($Shape<{ dates?: DateRange, location?: GoogleMapPosition }>) => void // ^ add `$Shape`};Corrected Example 这篇关于对象类型中缺少流属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 阿里云证书,YYDS!
05-25 07:30