使用界面Typescript扩展日期原型 [英] Extend Date prototype with a interface Typescript
本文介绍了使用界面Typescript扩展日期原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在javascript / typescript中的Date原型中添加一个getWeekNumber函数。我想用一个接口becease做,否则我得到一个错误,他不知道方法getWeekNumber()。
I want to add a getWeekNumber function to the Date prototype in javascript / typescript. I want to do it with an interface becease otherwise I get a error that he does not know the method getWeekNumber().
首先我尝试了一个标准的日期界面,像这样:
First I tried with a standart Date interface like this:
interface Date {
getWeekNumber(): number;
}
这已经解决了Date的所有方法都不能再调用。
This had the resolve that all the methods of the Date are not call able anymore.
我想知道有一种方法可以使用界面来扩展Date。
I want to know of there is a way to extend the Date with an interface.
推荐答案
可以这样做:
在DateExt.ts中:
in DateExt.ts:
interface Date
{
getWeekNumber: () => number;
}
Date.prototype.getWeekNumber = function()
{
return 123;//your calculations goes here
};
在您的app.ts中:
in your app.ts:
import './DateExt';
let a = new Date();
console.log(a.getWeekNumber());
这篇关于使用界面Typescript扩展日期原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文