# TS 的构造签名和构造函数类型是啥？傻傻分不清楚

### 一、构造签名

interface Point {
new (x: number, y: number): Point;
}


ConstructSignature:

new
TypeParametersopt
(
ParameterListopt
)
TypeAnnotationopt

ParameterListopt
TypeAnnotationopt

new C
new C ( ... )
new C  ( ... )


### 二、构造函数类型

An object type containing one or more construct signatures is said to be a
constructor type

. Constructor types may be written using constructor type literals or by including construct signatures in object type literals.

• 包含一个或多个构造签名的对象类型被称为构造函数类型；
• 构造函数类型可以使用构造函数类型字面量或包含构造签名的对象类型字面量来编写。

new  ( p1, p2, ... ) => R


{ new  ( p1, p2, ... ) : R }


// 构造函数字面量
new (x: number, y: number) => Point


{
new (x: number, y: number): Point;
}


### 三、构造函数类型的应用

interface Point {
new (x: number, y: number): Point;
x: number;
y: number;
}

class Point2D implements Point {

constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}

const point: Point = new Point2D(1, 2);


Class 'Point2D' incorrectly implements interface 'Point'.
Type 'Point2D' provides no match for the signature 'new (x: number, y: number): Point'.


interface Point {
x: number;
y: number;
}

interface PointConstructor {
new (x: number, y: number): Point;
}


class Point2D implements Point {

constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}

function newPoint(
pointConstructor: PointConstructor,
x: number,
y: number
): Point {
return new pointConstructor(x, y);
}

const point: Point = newPoint(Point2D, 2, 2);