【发布时间】:2016-08-22 23:21:05
【问题描述】:
我正在尝试使用 TypeScript 1.8 中新的全局增强来扩展原生 JavaScript 类型,如 here 所述。但是,当扩展函数返回相同类型时,我遇到了问题。
Global.ts
export {};
declare global {
interface Date {
Copy(): Date;
}
}
if (!Date.prototype.Copy) {
Date.prototype.Copy = function () {
return new Date(this.valueOf());
};
}
DateHelper.ts
export class DateHelper {
public static CopyDate(date: Date): Date {
return date.Copy();
}
}
我在尝试使用 DateHelper.ts 中定义的扩展时遇到以下错误 TS2322:
Type 'Date' is not assignable to type 'Date'.
Property 'toDateString' is missing in type 'Date'.
有人知道如何解决这个问题吗?
【问题讨论】: