2017-10-20 99 views
0

在来自标准Node.js库的类型定义中,我找到了接口DateConstructor的定义。TypeScript接口签名“():string”

interface DateConstructor { 
    new(): Date; 
    new(value: number): Date; 
    new(value: string): Date; 
    new(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): Date; 
    (): string; 
    readonly prototype: Date; 
    /** 
     * Parses a string containing a date, and returns the number of milliseconds between that date and midnight, January 1, 1970. 
     * @param s A date string 
     */ 
    parse(s: string): number; 
    /** 
     * Returns the number of milliseconds between midnight, January 1, 1970 Universal Coordinated Time (UTC) (or GMT) and the specified date. 
     * @param year The full year designation is required for cross-century date accuracy. If year is between 0 and 99 is used, then year is assumed to be 1900 + year. 
     * @param month The month as an number between 0 and 11 (January to December). 
     * @param date The date as an number between 1 and 31. 
     * @param hours Must be supplied if minutes is supplied. An number from 0 to 23 (midnight to 11pm) that specifies the hour. 
     * @param minutes Must be supplied if seconds is supplied. An number from 0 to 59 that specifies the minutes. 
     * @param seconds Must be supplied if milliseconds is supplied. An number from 0 to 59 that specifies the seconds. 
     * @param ms An number from 0 to 999 that specifies the milliseconds. 
     */ 
    UTC(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): number; 
    now(): number; 
} 

declare const Date: DateConstructor; 

它包含奇怪的定义(): string。我如何在课堂上定义它,想要实现这个接口?

+1

[TypeScript:具有构造函数的函数接口]的可能重复(https://stackoverflow.com/questions/46809986/typescript-functional-interface-with-constructor/46812163#46812163) – jcalz

回答

1

如果你觉得这个问题是来自the other one I linked to足够的不同:

这定义是指在类的构造函数也没有参数的时候没有new称为返回字符串可调用的函数。您不能使用ES2015或 - laterclass并遵守规范,因为在调用时不需要new就需要抛出TypeError。相反,您可以返回一个检测被调用的函数new,并添加额外的属性来实现静态方法。

我给你一个例子,我提供了一个围绕内建Date构造函数对象的包装。首先,让我们描述就像一个功能,有或没有的new关键字的接口部分:

interface FunctionalPartOfDateConstructor { 
    new(): Date; 
    new(value: number): Date; 
    new(value: string): Date; 
    new(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): Date; 
(): string; 
} 

现在让我们来尝试实现这一点部分:

const funcPart = function(valueOrYear?: number | string, month?: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): Date | string { 
    if (typeof new.target === 'undefined') { 
    // called as function 
    return Date(); 
    } 
    if (typeof valueOrYear === 'undefined') { 
    // called as constructor with no arguments 
    return new Date(); 
    } 
    if (typeof valueOrYear === 'string') { 
    // called as constructor with string value argument 
    return new Date(valueOrYear); 
    } 
    if (typeof month === 'undefined') { 
    // called as constructor with number value argument 
    return new Date(valueOrYear); 
    } 
    // called as constructor with year, month, date, etc arguments: 
    return new Date(valueOrYear, month, date, hours, minutes, seconds, ms); 
} as FunctionalPartOfDateConstructor; 

请注意,我用new.targetnew来检测函数是否被调用。我认为,这在针对ES5时合理的编译。并注意它必须区分所有不同的重载签名之间的区别。现在

我们可以通过合并的东西的功能部分,实现静态方法使全DateConstructor实例:

const myDateConstructor: DateConstructor = Object.assign(funcPart, { 
    prototype: Date.prototype, 
    parse(s: string) { 
    return Date.parse(s); 
    }, 
    UTC(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number) { 
    return Date.UTC(year, month, date, hours, minutes, seconds, ms); 
    }, 
    now() { 
    return Date.now(); 
    } 
}) 

您可以try it on the TypeScript Playground如果你想。希望有所帮助;祝你好运!

+0

谢谢。它对我非常有帮助。 – akazakou