reactjs - 打字稿错误“0”不可分配给类型 QueryKey
问题描述
type DateRange = { start: Date; end: Date }
type ParamType = {
latitude: number | null | undefined
longitude: number | null | undefined
term: string
dateRange: DateRange
}
const params: ParamType = {
latitude: geo && geo.lat,
longitude: geo && geo.lng,
term,
dateRange,
}
type QueryKey = (ParamType | string)[] | null | undefined
const key: QueryKey = geo && geo.lat && geo.lng && ['all-results-cts', params]
Typescript'0' not assignable to type QueryKey
为key
变量赋值引发错误。代码有什么问题?
解决方案
我认为这里有问题-
type QueryKey = (ParamType | string)**[]** | null | undefined
0
不可分配给类型数组或 null 或未定义。
(ParamType | string)**[]**
type 用于 (ParamType | string) 类型的数组。您需要检查获取键变量的类型并修复它或使用字符串类型调整 QueryKey。
type QueryKey = (ParamType | string)**[]** | null | undefined | string
推荐阅读
- javascript - 如何在本机反应中进行条件导航?
- attributes - 配置:错误:缺少 __attribute__ ((constructor)) 支持?
- regex - 正则表达式提取占位符值,默认排他
- php - php: 有没有办法从 SQL 字符串中获取数据库、表和字段列表?
- vba - tab键的vba正则表达式是什么?
- amazon-dynamodb - 有没有办法在 dynamoDb 表中为每个存储的记录创建不同的 ttl 记录
- react-native - React Navigation:关闭没有动画的当前堆栈并使用动画导航到父堆栈路由
- java - 如何在电子邮件中嵌入 Web 表单
- django - 基于单个不同列选择唯一行 - Django
- java - 如何从 Vaadin TreeGrid/Grid 的行和单元格中获取应用的背景色?