<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 中声明一个委托类型

        Declare a delegate type in Typescript(在 Typescript 中声明一个委托类型)

        <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 中声明一个委托类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

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

                    <tbody id='KyDHS'></tbody>

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

                            <bdo id='KyDHS'></bdo><ul id='KyDHS'></ul>
                          • <tfoot id='KyDHS'></tfoot>
                          • <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>