6

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.

1 Answer 1

3

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Are you sure this works ? In my TypeScript I can't distinguish like this between the NameSpace Name and my Class. ps. the link does not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.