未捕获Typescript函数的返回类型
我在这里缺少什么,未捕获Typescript函数的返回类型,typescript,Typescript,我在这里缺少什么,result的类型是{foo:unknown,bar:unknown,baz:unknown} 我认为它应该是{foo:number,bar:boolean,baz:string} export function apply<A, B extends {[P in keyof A]: B[P]}>( functions: {[P in keyof A]: (a: A[P]) => B[P]}, data: {[P in keyof A]: A[P]},
result
的类型是{foo:unknown,bar:unknown,baz:unknown}
我认为它应该是{foo:number,bar:boolean,baz:string}
export function apply<A, B extends {[P in keyof A]: B[P]}>(
functions: {[P in keyof A]: (a: A[P]) => B[P]},
data: {[P in keyof A]: A[P]},
): {[P in keyof A]: B[P]} {
const result = {} as {[P in keyof A]: B[P]}
const keys = Object.keys(functions) as (keyof A)[]
for(const key of keys)
result[key] = functions[key](data[key])
return result
}
const functions = {
foo: (a: string) => a.length,
bar: (a: number) => a === 42,
baz: (a: boolean) => a ? 'true' : 'false'
}
const data = {foo: 'foo', bar: 42, baz: true}
const result = apply(functions, data)
导出功能应用(
函数:{[P in keyof A]:(A:A[P])=>B[P]},
数据:{[P in keyof A]:A[P]},
):{[P在A的键中]:B[P]}{
const result={}作为{[P in keyof A]:B[P]}
const keys=Object.keys(函数)as(keyof A)[]
for(键的常数键)
结果[键]=函数[键](数据[键])
返回结果
}
常量函数={
foo:(a:string)=>a.length,
酒吧:(a:number)=>a==42,
baz:(a:boolean)=>a?'true':'false'
}
const data={foo:'foo',bar:42,baz:true}
常量结果=应用(函数、数据)
首先,让我们检查当前尝试的问题,即您为这两个参数指定的类型
functions: {[P in keyof A]: (a: A[P]) => B[P]},
data: {[P in keyof A]: A[P]}
看起来您希望类型A[p]
表示A
对象的值类型。这里的问题是对类型A
没有任何限制,因此我们无法使用它来推断更具体的值类型。因此,它们被推断为具有最通用的类型:unknown
考虑在调用apply
函数时可以手动指定通用参数。比如:
type A = {} // ??? what to put here?
type B = {} // ??? what to put here?
const result = apply<A, B>(functions, data)
对于B
,这是您的数据类型,它应该与A
中每个相应函数的参数类型相匹配。我们还可以在通用签名中应用该限制:
B extends {[P in keyof A]: Parameters<A[P]>[0]}
综上所述:
function apply<A extends Record<string, (a: any) => any>, B extends {[P in keyof A]: Parameters<A[P]>[0]}>(
functions: A,
data: B
): {[P in keyof A]: ReturnType<A[P]> {
const result = {} as {[P in keyof A]: B[P]}
const keys = Object.keys(functions) as (keyof A)[]
for(const key of keys)
result[key] = functions[key](data[key])
return result
}
const functions = {
foo: (a: string) => a.length,
bar: (a: number) => a === 42,
baz: (a: boolean) => a ? 'true' : 'false'
}
const data = {foo: 'foo', bar: 42, baz: true}
const result = apply(functions, data)
// The type of `result` is inferred to be:
// {
// foo: number;
// bar: boolean;
// baz: "true" | "false";
// }
函数apply,B扩展了{[P in keyof A]:参数[0]}>(
职能:A,,
数据:B
):{[P in keyof A]:返回类型{
const result={}作为{[P in keyof A]:B[P]}
const keys=Object.keys(函数)as(keyof A)[]
for(键的常数键)
结果[键]=函数[键](数据[键])
返回结果
}
常量函数={
foo:(a:string)=>a.length,
酒吧:(a:number)=>a==42,
baz:(a:boolean)=>a?'true':'false'
}
const data={foo:'foo',bar:42,baz:true}
常量结果=应用(函数、数据)
//“result”的类型推断为:
// {
//foo:数字;
//条形图:布尔型;
//baz:“真”|“假”;
// }
{[P in keyof A]: ReturnType<A[P]>
function apply<A extends Record<string, (a: any) => any>, B extends {[P in keyof A]: Parameters<A[P]>[0]}>(
functions: A,
data: B
): {[P in keyof A]: ReturnType<A[P]> {
const result = {} as {[P in keyof A]: B[P]}
const keys = Object.keys(functions) as (keyof A)[]
for(const key of keys)
result[key] = functions[key](data[key])
return result
}
const functions = {
foo: (a: string) => a.length,
bar: (a: number) => a === 42,
baz: (a: boolean) => a ? 'true' : 'false'
}
const data = {foo: 'foo', bar: 42, baz: true}
const result = apply(functions, data)
// The type of `result` is inferred to be:
// {
// foo: number;
// bar: boolean;
// baz: "true" | "false";
// }