Typescript overload arrow functions

前端 未结 3 473
夕颜
夕颜 2020-12-08 19:50

So we can do:

export function myMethod (param: number) :number
export function myMethod (param: string) :string

export function myMethod (param: string | nu         


        
3条回答
  •  余生分开走
    2020-12-08 20:31

    The declaration of overloaded signatures is always

    function name(args...): result;
    

    with a function keyword and a function name.

    Your syntax

    var myMethodArror = (param: string): string;
    

    is invalid. It is trying to assign something that looks like the beginning of an arrow function to a variable, but the function has no body. You will get the error

    '=>' expected

    If you repeat this with a a different signature, then you'll also get a duplicate property error, or perhaps the error

    Subsequent variable declarations must have the same type.

    This is not specific to arrow functions. The same problem would arise if you tried to do

    var myMethodArror = function(param: string): string;
    

    which would yield

    '{' expected

    since the function body is missing.

提交回复
热议问题