javascript - Flow 不允许我将具有更多属性的泛型传递给期望更少属性的子级
问题描述
我有一个在多个地方使用的通用类型。
有时我必须编写使用相同泛型的稍微不同版本的组件(基本上相同的泛型但使用较少的道具)。我希望能够将具有更多道具的对象传递给期望它们具有更少道具的组件,但流程不允许我这样做。
因此,对于这样一个非常简单的泛型:
export type Form<T: {} = {}> = {
data: T,
}>
我应该能够像这样传递它:
type MyData = $ReadOnly<{ cool: boolean, superCool: boolean }>
type MyOtherData = $ReadOnly<{ cool: boolean }>
type OProps = {
form: Form<MyOtherData>,
}
const OPstuff = function(p:OProps){
return <div>Soy yo</div>
}
type PProps = {
form: Form<MyData>,
stuff: string[]
}
const PPstuff = function(p:PProps){
return <OPstuff form={p.form}></OPstuff>
}
但是 flow 抱怨 parent 有更多的 props,这应该不是问题。
起初我认为这可能是一个突变问题,毕竟这是有道理的,但是在将所有内容注释为只读之后,我得到了同样的错误:
export type Form<T: $ReadOnly<{}> = {}> = $ReadOnly<{
data: $ReadOnly<T>,
}>
错误:
28: return <OPstuff form={p.form}>Soy puto</OPstuff>
^ Cannot create `OPstuff` element because property `superCool` is missing in object type [1] but exists in object type [2] in type argument `T` [3] of property `form`. [prop-missing]
References:
13: form: Form<MyOtherData>,
^ [1]
22: form: Form<MyData>,
^ [2]
4: export type Form<T: $ReadOnly<{}> = {}> = $ReadOnly<{
解决方案
推荐阅读
- winapi - Win32 DateTime_SetMonthCalColor 似乎不起作用
- c - C中函数声明的位置会影响分支预测吗?
- r - hexbin 和 ggplot 在不同的面板中
- aws-lambda - 如何向我的 alexa 技能发送 http 请求并从中获得响应?
- javascript - 如何根据数据字段动态显示我的元素?
- android - 为什么我的 Android Studio 语言变得很奇怪?
- c# - URL 重定向到变量
- c - 我试图在 VS 中运行一个简单的 c 代码,但这表明,该怎么办?
- angular - Angular 新的日期范围过滤器错误?
- windows - 无法使用 docker-compose 将 docker 文件夹挂载到主机中