Defining TypeScript callback type

后端 未结 8 1150
情话喂你
情话喂你 2020-12-12 11:53

I\'ve got the following class in TypeScript:

class CallbackTest
{
    public myCallback;

    public doWork(): void
    {
        //doing some work...
               


        
8条回答
  •  悲哀的现实
    2020-12-12 12:21

    You can use the following:

    1. Type Alias (using type keyword, aliasing a function literal)
    2. Interface
    3. Function Literal

    Here is an example of how to use them:

    type myCallbackType = (arg1: string, arg2: boolean) => number;
    
    interface myCallbackInterface { (arg1: string, arg2: boolean): number };
    
    class CallbackTest
    {
        // ...
    
        public myCallback2: myCallbackType;
        public myCallback3: myCallbackInterface;
        public myCallback1: (arg1: string, arg2: boolean) => number;
    
        // ...
    
    }
    

提交回复
热议问题