我收到此错误消息,不知道该怎么办(如下面的EDIT 所述,我使用的是 TS version 3.9.0-dev.20200324
) ...
error(TS2345): Argument of type 'awaited T' is not assignable to parameter of type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to 'awaited T'.
TypeScript 代码:lib/functional/promise-or-not.ts
// ... [more code]
export async function thenified<T>(
promise: Promise<T>,
funct: <R>(t: T) => R,
): Promise<any> {
return promise.then(
t => funct(t)
);
}
输出:tsc Version 3.9.0-dev.20200324
% ❯ tsc # 3.9.0-dev.20200324
lib/functional/promise-or-not.ts:10:16 - error TS2345: Argument of type 'awaited T' is not assignable to parameter of type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'awaited T'.
10 t => funct(t)
~
Found 1 error.
另见 this comment在 Microsoft/TypeScript/#37664 中:
Wrong return type inference in a generic function using Promise.all
编辑
由于在使用其他 TS 版本时我的代码中存在不同的其他问题,我无法使用 Version 3.9.0-dev.20200324
。
这是在使用Version 4.0.0-dev.20200504
时其他部分的代码这里就不展示了,问题详见this GitHub issue .
最佳答案
将 t
转换为 any
export async function thenified<T>(
promise: Promise<T>,
funct: <R>(t: T) => R,
): Promise<any> {
return promise.then(
t => funct(t as any)
);
}
糟糕的解决方法,但你能做什么。
关于typescript - 类型 'awaited T' 的参数不可分配给类型 'T' 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61602377/