javascript - 调用泛型函数时使 Flow 需要类型
问题描述
如何使 Flow 要求在调用函数时指定泛型类型?
// @flow strict
type Person = {
_id: string,
name: string,
age: number,
}
type WrapperObj = {
genericFn<T>(string): T | null,
}
function testFunc (obj: WrapperObj, name: string) {
// Goal: have flow produce error on the following line because generic is missing...
const jack = obj.genericFn(name)
if (jack) {
// ...so that this line produces an error
jack.height
}
}
如果我以以下任何一种方式在第 15 行提供类型:
const jack = obj.genericFn<Person>(name)
const jack: Person | null = obj.genericFn(name)
...然后 Flow 正确地在第 18 行给出了我所期望的错误。
Flow 似乎在推断类型T
,因为any
实际上我们希望它抱怨缺少类型。
解决方案
推荐阅读
- mysql - 根据彼此的插入 ID 在 2 个表中插入行
- airflow-scheduler - 在 Airflow 2.0 中按需将 DAG 序列化为 DB
- python - 格式化 Matplotlib 刻度数(千)。TypeError:“str”和“int”的实例之间不支持“<”
- rust - 如何理解 FusedFuture
- r - 如何通过分类数据绘图来控制 R 中变量的某些值?
- android - Firefox 移动设备上的 SSL 证书始终来自“缓存”
- react-native - React Native Webview 身份验证
- php - 使用枚举的 Doctrine 数组并将其存储在单独的表中
- python - 用基于条件的值替换熊猫数据框中的列切片中的值
- arrays - 当大小未知时,通过引用将 char 数组传递给函数