Declare a delegate type in Typescript

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

    Type definition for a callable expression (this is a draft ok, for humans... not a BNF or anything formal):

    callableType: (paramsDef) => returnType
    
    paramsDef:    MULTIPLE paramDef SEPARATED BY ,
    
    paramDef:     EITHER   paramName: paramType
                      OR   optionalParamName?: paramTypeWhenDefined
                      OR   ...manyParamName: eachParamType[]
    

    Example:

    var func = something as ((...x: any[]) => any);
    

    Then you can:

    var result = func("a", "b", 2);
    

提交回复
热议问题