Defining TypeScript callback type

后端 未结 8 1154
情话喂你
情话喂你 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:02

    I just found something in the TypeScript language specification, it's fairly easy. I was pretty close.

    the syntax is the following:

    public myCallback: (name: type) => returntype;
    

    In my example, it would be

    class CallbackTest
    {
        public myCallback: () => void;
    
        public doWork(): void
        {
            //doing some work...
            this.myCallback(); //calling callback
        }
    }
    

提交回复
热议问题