0

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.

How can I make it work?

1 Answer 1

1

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. 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.