admin管理员组文章数量:1327967
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;
}
This had the resolve that all the methods of the Date are not call able anymore.
I want to know of there is a way to extend the Date with an interface.
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;
}
This had the resolve that all the methods of the Date are not call able anymore.
I want to know of there is a way to extend the Date with an interface.
Share Improve this question edited Mar 30, 2016 at 9:10 Amid 22.4k6 gold badges62 silver badges58 bronze badges asked Mar 30, 2016 at 8:53 Martijn BakkerMartijn Bakker 3951 gold badge5 silver badges17 bronze badges1 Answer
Reset to default 9You can do it this way:
in DateExt.ts:
interface Date
{
getWeekNumber: () => number;
}
Date.prototype.getWeekNumber = function()
{
return 123;//your calculations goes here
};
in your app.ts:
import './DateExt';
let a = new Date();
console.log(a.getWeekNumber());
本文标签: javascriptExtend Date prototype with a interface TypescriptStack Overflow
版权声明:本文标题:javascript - Extend Date prototype with a interface Typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742225622a2436259.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论