Lokomotywa
Lokomotywa

Reputation: 2844

cannot use 'new' with an expression whose type lacks a call or construct signature - but it is defined

I have problem with a 'new' constructor in an interface definition:

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);

in the last line, the compiler complains cannot use 'new' with an expression whose type lacks a call or construct signature. Why? Did not I declare exactly that in line 2?

Upvotes: 0

Views: 104

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249516

The definition of your constructor signature is wrong, you define a field named new which is a function. This is how constructor signature should look:

interface DateOnlyStatic {
    new (y: number, m: number, d: number) : DateOnly;
}

Upvotes: 3

Related Questions