Flowtype 类型参数上的流$ObjMap
流没有产生我在下面所期望的错误。这是流的错误(可能),还是我误解了类型参数Flowtype 类型参数上的流$ObjMap,flowtype,Flowtype,流没有产生我在下面所期望的错误。这是流的错误(可能),还是我误解了类型参数 function test<A: Object>( obj: A, mapper: $ObjMap<A, <V>(V) => V => any> ) { } test( { foo: 1, bar: '2', }, { foo: (x: number) => String(x), bar: (x: number)
function test<A: Object>(
obj: A,
mapper: $ObjMap<A, <V>(V) => V => any>
) {
}
test(
{
foo: 1,
bar: '2',
},
{
foo: (x: number) => String(x),
bar: (x: number) => parseInt(x),
// ^^^^^^
// should error that this is the wrong argument type
}
)
功能测试(
obj:A,
映射器:$ObjMap V=>any>
) {
}
试验(
{
傅:1,,
酒吧:"2",,
},
{
foo:(x:number)=>字符串(x),
条:(x:number)=>parseInt(x),
// ^^^^^^
//错误应该是这是错误的参数类型
}
)
这看起来像是一个bug,因为以下内容会产生错误,尽管发生在错误的位置:
const a = {foo: 1, bar: '2'}
type A = typeof a
type B = $ObjMap<A, <V>(V) => V => any>
const b: B = {
foo: x => String(x),
bar: (x: number) => parseInt(x),
}
const a={foo:1,bar:'2'}
A型=A型
类型B=$ObjMap V=>any>
常数b:b={
foo:x=>String(x),
条:(x:number)=>parseInt(x),
}
错误:
3: type B = $ObjMap<A, <V>(V) => V => any>
^ Cannot instantiate `$ObjMap` because string [1] is incompatible with number [2] in the first argument.
References:
1: const a = {foo: 1, bar: '2'}
^ [1]
7: bar: (x: number) => parseInt(x),
^ [2]
3:type B=$ObjMap V=>any>
^无法实例化“$ObjMap”,因为字符串[1]与第一个参数中的数字[2]不兼容。
参考资料:
1:const a={foo:1,bar:'2'}
^ [1]
7:bar:(x:number)=>parseInt(x),
^ [2]
好的,我可以让它工作了
declare function test<A: Object, M: $ReadOnly<$ObjMap<A, <I, O>(I) => I => O>>>(
obj: A,
mapper: M
): $ObjMap<M, <I, O>((I) => O) => O>
声明函数测试>>(
obj:A,
制图员:M
):$ObjMap O)=>O>
这并不理想,因为输出只能分配给
$ReadOnly
形状,但它涵盖了我需要的所有类型检查。我真的不能说这是否是一个bug,但如果您使用mapper:$ReadOnly any>
的话,它似乎可以工作,但是请注意,Object
是一种不安全的类型,现在只是any
的别名。我建议删除它和any
类型,因为它们是为了绕过类型系统而设计的。使用这些不安全的类型时,您可能会遇到不安全的类型检查行为。@但是,所有映射器函数都可以有不同的返回类型,那么我如何才能要求映射器的所有值都是函数而不限制其返回类型?似乎我实际上可以这样做$ObjMap V=>R>
,但我不确定这是否真的符合犹太教义