Declare a delegate type in Typescript

前端 未结 5 681
走了就别回头了
走了就别回头了 2021-01-30 10:12

Coming from a C# background, I want to create a datatype that defines a function signature. In C#, this is a delegate declared like this:

delegate v         


        
5条回答
  •  醉话见心
    2021-01-30 10:48

    In TypeScript, interfaces can have call signatures. In your example, you could declare it like this:

    interface Greeter {
        (message: string): void;
    }
    
    function sayHi(greeter: Greeter) {
        greeter('Hello!');
    }
    
    sayHi((msg) => console.log(msg)); // msg is inferred as string
    

提交回复
热议问题