Extend Date prototype with a interface Typescript

前端 未结 1 346
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 10:03

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 k

相关标签:
1条回答
  • 2020-12-18 10:39

    You 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());
    
    0 讨论(0)
提交回复
热议问题