[英]typescript callback type mismatch
I have this code in callback many places : 我在许多地方的回调中都有此代码:
return new Promise<Result> (
(resolve : (Result ) =>void,reject: ( any) =>void) =>{
.......
});
I thought I will create an interface for this long type : 我以为我会为此长类型创建一个接口:
interface callback<T> {
resolve : (value? :T ) =>void;
reject : (error? : any) =>void;
}
But I cannot use it in place like : 但是我不能在这样的地方使用它:
return new Promise<Result> (
( c : Callback<Result> ) = > {
......
}
TS complains that Callback is not a resolve: Result => void. TS抱怨Callback无法解决:结果=>无效。
How can I make it work? 我该如何运作?
Promises are already typed if you're targeting ES6, there are typings for shims if you target ES5, and typings for non-native Promise libs. 如果您以ES6为目标,则已经输入了承诺;如果以ES5为目标,则已经输入了垫片;对于非本地Promise库,则有键入。 So, you shouldn't need to write your own type definition for this.
因此,您不需要为此编写自己的类型定义。 When constructing a promise this is the only code you need to write:
构造诺言时,这是您唯一需要编写的代码:
new Promise<TypeOfResult>((resolve, reject) => {
// do yar thing
})
TypeScript will infer the type of resolve
and reject
so specifying their type explicitly is redundant and needlessly verbose. TypeScript会推断出
resolve
和reject
的类型,因此明确指定其类型是多余且不必要的冗长。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.