I'd like to declare a class called Date
which has a property of type Date (as in the TypeScript interface to the JavaScript Date object. However the compiler assumes my property is of the same type as the class I'm declaring. How can I distinguish between the two?
If the Date interface were in a module I could just use the module name to distinguish, however it seems to be in a global namespace. My Date
class is inside a module.
I believe there is no special keyword to access the global namespace, but the following works:
// Create alias (reference) to the global Date object
var OriginalDate = Date;
// Make copy of global Date interface
interface OriginalDate extends Date {}
module Foo {
export class Date {
public d: OriginalDate; // <-- use alias of interface here
constructor() {
this.d = new OriginalDate(2014, 1, 1); // <-- and reference to object here
}
}
}
var bar = new Foo.Date();
alert(bar.d.getFullYear().toString());
See also: https://github.com/Microsoft/TypeScript/blob/master/src/lib/core.d.ts
In the past, I've always named such classes "DateTime" to avoid this problem (and possible confusion).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With