<bdo id='QpGBo'></bdo><ul id='QpGBo'></ul>
  • <i id='QpGBo'><tr id='QpGBo'><dt id='QpGBo'><q id='QpGBo'><span id='QpGBo'><b id='QpGBo'><form id='QpGBo'><ins id='QpGBo'></ins><ul id='QpGBo'></ul><sub id='QpGBo'></sub></form><legend id='QpGBo'></legend><bdo id='QpGBo'><pre id='QpGBo'><center id='QpGBo'></center></pre></bdo></b><th id='QpGBo'></th></span></q></dt></tr></i><div id='QpGBo'><tfoot id='QpGBo'></tfoot><dl id='QpGBo'><fieldset id='QpGBo'></fieldset></dl></div>
  • <tfoot id='QpGBo'></tfoot>

    1. <small id='QpGBo'></small><noframes id='QpGBo'>

      1. <legend id='QpGBo'><style id='QpGBo'><dir id='QpGBo'><q id='QpGBo'></q></dir></style></legend>

        在 Typescript 中声明一个委托类型

        时间:2023-11-11

        <tfoot id='tyyhk'></tfoot>

      2. <i id='tyyhk'><tr id='tyyhk'><dt id='tyyhk'><q id='tyyhk'><span id='tyyhk'><b id='tyyhk'><form id='tyyhk'><ins id='tyyhk'></ins><ul id='tyyhk'></ul><sub id='tyyhk'></sub></form><legend id='tyyhk'></legend><bdo id='tyyhk'><pre id='tyyhk'><center id='tyyhk'></center></pre></bdo></b><th id='tyyhk'></th></span></q></dt></tr></i><div id='tyyhk'><tfoot id='tyyhk'></tfoot><dl id='tyyhk'><fieldset id='tyyhk'></fieldset></dl></div>

            <tbody id='tyyhk'></tbody>
            <bdo id='tyyhk'></bdo><ul id='tyyhk'></ul>

              1. <small id='tyyhk'></small><noframes id='tyyhk'>

                  <legend id='tyyhk'><style id='tyyhk'><dir id='tyyhk'><q id='tyyhk'></q></dir></style></legend>
                  本文介绍了在 Typescript 中声明一个委托类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  来自 C# 背景,我想创建一个定义函数签名的数据类型.在 C# 中,这是一个 delegate 声明如下:

                  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 void Greeter (string message);
                  
                  public class Foo
                  {
                      public void SayHi (Greeter g) {
                          g("Hi!");
                      }
                  }
                  

                  现在,我想在 Typescript 中实现类似的功能.我知道 Typescript 没有委托类型,但只有 lambdas.我想出了这样的事情:

                  Now, I want to achieve similar in Typescript. I know Typescript has no delegate types, but only lambdas. I came up with something like this:

                  class Foo {
                      SayHi (greeter: (msg: String) => void) {
                          greeter('Hi!');
                      }
                  }
                  

                  虽然这可行,但我想重用方法签名 (msg:String) =>void 几次,并认为创建自定义类型会更简洁 - 就像 C# 中的委托一样.

                  While this works, I want to reuse the method signature (msg:String) => void couple of times and think it would be cleaner to create a custom type - like the delegate in C#.

                  有什么想法可以做到这一点吗?

                  Any ideas how this can be done?

                  推荐答案

                  在 TypeScript 中,接口可以有调用签名.在您的示例中,您可以这样声明:

                  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
                  

                  这篇关于在 Typescript 中声明一个委托类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将 IronPython 方法分配给 C# 委托 下一篇:Func&lt;string,string&gt; 有什么区别?和委托?

                  相关文章

                • <legend id='KyDHS'><style id='KyDHS'><dir id='KyDHS'><q id='KyDHS'></q></dir></style></legend>

                    1. <small id='KyDHS'></small><noframes id='KyDHS'>

                        <bdo id='KyDHS'></bdo><ul id='KyDHS'></ul>
                    2. <tfoot id='KyDHS'></tfoot>
                    3. <i id='KyDHS'><tr id='KyDHS'><dt id='KyDHS'><q id='KyDHS'><span id='KyDHS'><b id='KyDHS'><form id='KyDHS'><ins id='KyDHS'></ins><ul id='KyDHS'></ul><sub id='KyDHS'></sub></form><legend id='KyDHS'></legend><bdo id='KyDHS'><pre id='KyDHS'><center id='KyDHS'></center></pre></bdo></b><th id='KyDHS'></th></span></q></dt></tr></i><div id='KyDHS'><tfoot id='KyDHS'></tfoot><dl id='KyDHS'><fieldset id='KyDHS'></fieldset></dl></div>