Unable to get value of the property 'elementType': object is null or undefined-Record and share programming errors

I have the following code in a TypeScript file:

define('myModule', [], function () {

    var self = {
        init: function () {
            doSomething();
        }
    };

    var dayNames:string[] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

    function doSomething () {
        var current = new Date();
        var day = dayNames[current.getDay()];
    }

    return self;
});

However it does not compile with Visual Studio 2012/Web Essentials 2012.

Compile Error. See error list for details tsc.js(19148, 13) Microsoft JScript runtime error: Unable to get value of the property ‘elementType’: object is null or undefined

Strangely the same code seems to compile fine on the TypeScript playground. I have checked the version of C:\Program Files (x86)\Microsoft SDKs\TypeScript\tsc.js which is the current latest on CodePlex (0.8.3.0).

If I comment out the following line, it compiles fine:

var day = dayNames[current.getDay()];

And the following code block in isolation also compiles fine:

var dayNames:string[] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

function doSomething () {
    var current = new Date();
    var day = dayNames[current.getDay()];
}

So what am I doing wrong?

  1. If you move the definition of dayNames before the self declaration it compiles fine so it appears you’re calling doSomething() before dayNames is defined.

    Why this is the case, and why it doesn’t generate a more helpful compiler error I’m not sure – I’d put that down to TypeScript still being alpha code. You may want to raise it as an issue on CodePlex.

    I have noticed a few other instances like this. For example if you have one class which extends another in the same file, you need to put the base class first, otherwise the compiler sometimes falls over.

  2. This seems to be resolved after updating to TypeScript 0.9.0.

Originally posted 2013-08-31 07:07:00.