不能将'new'与其类型缺少调用或构造签名的表达式一起使用 - 但它已定义

时间:2018-02-14 14:37:44

标签: typescript

我在接口定义中遇到'new'构造函数的问题:

interface DateOnlyStatic {
    new: (y: number, m: number, d: number) => DateOnly;
    today: () => DateOnly;
    fromDate(d: DateOnly): DateOnly;
    fromISOString(s: string): DateOnly;
    fromBaseDateTime(a: any): any;
    areEqual(d1: DateOnly, d2: DateOnly): boolean;
    comparer(d1: DateOnly, d2: DateOnly): boolean;
}


interface DateOnly extends Date {
    monday: () => DateOnly;
    addDays: (i: number) => DateOnly;
    addMonths: (i: number) => DateOnly;
    getTotalDays: () => number;

}

declare var DateOnly: DateOnlyStatic;


declare var g = new DateOnly(2001, 1, 1);

在最后一行,编译器抱怨cannot use 'new' with an expression whose type lacks a call or construct signature。为什么?我没有在第2行确切地说明这一点吗?

1 个答案:

答案 0 :(得分:3)

构造函数签名的定义是错误的,您定义了一个名为new的字段,它是一个函数。这是构造函数签名的外观:

<span class="updating_span">  </span>