Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 函数重载,参数问题_Typescript_Overloading - Fatal编程技术网

Typescript 函数重载,参数问题

Typescript 函数重载,参数问题,typescript,overloading,Typescript,Overloading,在此代码中,声明了几个重载函数: export function rect(a: Point, b: Point): Rect; export function rect(a: Point, b: number|Point, c?: number): Rect; export function rect(a: number|Point, b: number|Point, c?: number, d?: number ) {return new Rect(a,b,c,d);} rect( 1,1,

在此代码中,声明了几个重载函数:

export function rect(a: Point, b: Point): Rect;
export function rect(a: Point, b: number|Point, c?: number): Rect;
export function rect(a: number|Point, b: number|Point, c?: number, d?: number ) {return new Rect(a,b,c,d);}
rect( 1,1,1, 1)   // ts: "Expected 2-3 arguments, but got 4.ts(2554)"
Typescript在第4个参数中抱怨。我不知道这里出了什么问题。如果我再添加一个重载和一个参数:

export function rect(a: Point, b: Point): Rect;
export function rect(a: Point, b: number|Point, c?: number): Rect;
export function rect(a: number|Point, b: number|Point, c?: number, d?: number ): Rect ;
export function rect(a: number|Point, b: number|Point, c?: number, d?: number, e?: number ): Rect {return new Rect(a,b,c,d);}
rect( 1,1,1, 1)
ts没有抱怨(如果我将第5个参数添加到对rect的调用中,它会给出相同的错误)


怎么了?

最后一个重载签名是实现签名,外部不可见。如果要使该接口公开,则需要复制该接口:

export function rect(a: Point, b: Point): Rect;
export function rect(a: Point, b: number | Point, c?: number): Rect;
export function rect(a: number | Point, b: number | Point, c?: number, d?: number): Rect;
export function rect(a: number | Point, b: number | Point, c?: number, d?: number) {
    return new Rect(a, b, c, d);
}

函数实现必须满足所有重载拒绝。如果需要更多参数,请添加另一个重载