javascript - 字符串与数字不兼容(默认为流类型可选属性)
问题描述
给定以下伪 React 组件:
type Props = {
width?: number | string
};
const Component = ({
width = '100%'
}: Props) => ( /* component body */ )
Flow 引发以下错误:
6: width = '100%'
^ string [1] is incompatible with number [2].
References:
2: width?: number | string
^ [1]
2: width?: number | string
^ [2]
6: width = '100%'
^ string [1] is incompatible with number [2].
References:
6: width = '100%'
^ [1]
2: width?: number | string
^ [2]
该组件应接受 3 种可能的类型width
:void | number | string
. 有正确的输入方法吗?我可以通过删除默认值来修复错误'100%'
,但该解决方案不太惯用。
解决方案
Flow 中有一个已知错误,当函数具有类型为联合的参数的默认参数时,会导致报告错误的类型错误。
您可以像这样解决它:
const Component = ({ width }: Props) => ( /* component body */ )
Component.defaultProps = {
width: '100%',
}
推荐阅读
- python - 我们如何使用 pandas 连接两个没有公共列的不同表
- sql - 有没有办法为 FullCalendar 执行添加编辑删除读取?
- java - Java EE 7:按名称模式注入所有 bean 实例
- c# - 更新 JSON 文件而不是添加
- javascript - React-native - 使用抽屉导航器传递参数
- shopify - Shopify - 对收藏列表进行分页
- java - PowerMockito verifyNew withArguments 对象和对象数组
- android - 在android中使用Retrofits2将文件上传到服务器
- python - Python将来自while循环的元组加入列表
- c# - 如何将附加文件附加到 NBug