1

How would you tell TypeScript to use Datejs as Date instead of the builtin Date object?

I tried adding a reference to the js file and declaring Date as type any i.e. declare var Date: any; but this did nothing to fix the error.

EDIT: I found this question, but it talks about specifying a method to include. I'm wondering if it's possible to leave it open ended and simply turn off type checking for Date;

Community
  • 1
  • 1
Jared
  • 5,840
  • 5
  • 49
  • 83

3 Answers3

7

To get you started with extending the native date, it would look like this:

interface DateIs {
    monday(): bool;
    // ctd...
    january(): bool;
    // ctd...
    weekday(): bool;
}

interface DateAdd {
    days(): Date;
    months(): Date;
    // ctd...
}

interface Date {
    parse(date: string): Date;
    today(): Date;
    next(): Date;
    last(): Date;
    monday(): Date;
    // ctd...
    january(): Date;
    // ctd...
    addDays(days: number): Date;
    addMonths(months: number): Date;
    add(quantity: number): DateAdd;
    is(): DateIs;
}

With this example, you could continue to add the functions you use, I have put in one month as an example so you could fill in february() and also the short jan() variations as you see fit!

Fenton
  • 241,084
  • 71
  • 387
  • 401
6

There isn't a way to take an existing variable with a type (Date in this case) and turn it in to an any.

The best option would be to extend the Date interface with the new methods from datejs. Hopefully at some point someone will do this and upload it to DefinitelyTyped.

Two workarounds you could do:

var Date2 = <any>Date;
// Use Date2 anywhere you would use Date

The other workaround would be to just modify lib.d.ts to declare Date as any. Obviously this will have side effects everywhere, but would work.

Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235
  • Hmmm, I'll try the `Date2` method. I had tried to write an interface declaration for only the methods that I needed to get started, but I'm doing something incorrectly I believe. Do you happen to know what that interface declaration would look like? – Jared Jan 14 '13 at 22:17
  • It's likely you were hitting the bug where extensions of things declared in lib.d.ts don't work well in Visual Studio. The interface would just look like a normal interface declaration. – Ryan Cavanaugh Jan 14 '13 at 22:33
  • Would you have to `extend` `Date` or simply `interface Date{}`? Also the `var Date2` worked great so ty. – Jared Jan 14 '13 at 22:44
  • e.g. `interface Date { getTimeSinceYesterday(): number; }` – Ryan Cavanaugh Jan 14 '13 at 22:45
  • It's on DefinitelyTyped: https://github.com/borisyankov/DefinitelyTyped/tree/master/datejs – David Sherret Jan 12 '14 at 01:55
1

Install the package 'datejs.typescript.definitelytyped' from nuget

http://www.nuget.org/packages/datejs.TypeScript.DefinitelyTyped/

Add a reference to the type file. In a global script file declare a global variable, or in a class declare either a private static member variable or simply a local variable, assigning as shown in the code below.

/// <reference path="typings/datejs/datejs.d.ts" />
var DateJs: IDateJSStatic = <any>Date;

I'm currently writing SPA applications so I use a global variable as shown in the code example.

Richard Collette
  • 5,462
  • 4
  • 53
  • 79