Declare a delegate type in Typescript

前端 未结 5 675
走了就别回头了
走了就别回头了 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 11:06

    I now publish and use @steelbreeze/delegate; it has a few limitations compared to the C# delegate, in that it's immutable, but otherwise works well (and when called returns results from all the functions called).

    It lets you write code such as:

    import { create as delegate } from "@steelbreeze/delegate";
    
    function world(s: string) {
        console.log(s + " world");
    }
    
    const one = delegate(s => console.log(s + " Hello world"));
    const two = delegate(s => console.log(s + " Hello"), world);
    
    one("A");
    two("B");
    
    delegate(one, two)("C");
    

提交回复
热议问题