4

This typescript:

export enum UID {
    FACTORY,
    ROBOT
}

compiles to this javascript:

(function (UID) {
    UID._map = [];
    UID._map[0] = "FACTORY";
    UID.FACTORY = 0;
    UID._map[1] = "ROBOT";
    UID.ROBOT = 1;
})(exports.UID || (exports.UID = {}));
var UID = exports.UID;

I have to admit that the code seems rather obscure to me but I trusted the tsc compiler to know what it's doing. Unfortunately the javascript can't be executed. nodejs complains that:

(function (UID) {

^ TypeError: object is not a function

at ...

What have I done wrong ?

UPDATE: Matt B. has solved the problem. This is a known bug in the typescript compiler. tsc fails to insert semicolons after require statements, this can lead to strange errors. Manually adding the semicolons to my code solved the problem. Here's the link to the codeplex issue: http://typescript.codeplex.com/workitem/364

UPDATE 2: for those of you that experience the same error. You can insert the missing semicolons manually but that is not a very comfortable solution since you have to do this after every compilation. I noted that the problem only occurs with the enum. There are lots of other modules in the project and none of them have caused this error. Apparently a class definition is not "harmed" by the missing semicolons in front of it. Just move the definition of the enum behind one of your class definitions and the error should disappear. It's not sufficient to move the enum behind an interface since interfaces have no direct equivalent and are just deleted by the compiler

10
  • 2
    Is that the whole script? Commented Mar 6, 2013 at 16:57
  • 3
    That second ) (in your error message) shouldn't be there: (function (UID)) {. The code portion works fine: jsfiddle.net/antisanity/cRNeN Commented Mar 6, 2013 at 16:59
  • 1
    incorrect concatenation with another file? Commented Mar 6, 2013 at 17:01
  • 1
    The code in the error message is not the same as the code posted above it. The error contains the double ), not the sample posted. Commented Mar 6, 2013 at 17:02
  • 1
    And it's not needed because you're in node.js so exports is already defined. Commented Mar 6, 2013 at 17:29

3 Answers 3

7

I think this is the same issue as described here -- which turned out to be due to a missing semicolon after a require() statement:

Typescript generating javascript that doesn't work

Is there a line like this in another compiled file?

var UID = require('UID')

If so, try adding a semicolon at the end:

var UID = require('UID');

This appears to be a TypeScript bug; here's the bug report (vote it up!): http://typescript.codeplex.com/workitem/364

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

3 Comments

yes, that solved the problem. Thank you for your answer. I upvoted the issue on codeplex, too
In case others want to upvote the bug report as well, here's the link: typescript.codeplex.com/workitem/364
nice, Ill add the link to the question
0

In your Javascript you should have something like

var exports = exports || {};

Before

(function (UID) {
    UID._map = [];
    UID._map[0] = "FACTORY";
    UID.FACTORY = 0;
    UID._map[1] = "ROBOT";
    UID.ROBOT = 1;
})(exports.UID || (exports.UID = {}));
var UID = exports.UID;

2 Comments

but the javascript was not generated by me. Does that mean that the typescript compiler is responsible for this error ?
The global exports reference is native to the Node.js environment and does not need to be declared.
0

I tried and I have the same problem - I assume that the missing exports variables should be generated by the JavaScript code used to load the module when you do

import XXXX = module("XXXX");

that generates this JavaScript:

var XXXX = require("./XXXX")

and I think Matt B. is right and the problem there is the missing semi-colon after require(), that messes things up afterwards.

A fix is to place the enumeration declaration in a module:

module Test {

    export enum UID {
      FACTORY,
      ROBOT
    }

}

that generates:

var test;
(function (test) {
    (function (UID) {
        UID._map = [];
        UID._map[0] = "FACTORY";
        UID.FACTORY = 0;
        UID._map[1] = "ROBOT";
        UID.ROBOT = 1;
    })(test.UID || (test.UID = {}));
    var UID = test.UID;
})(test || (test = {}));

in this way the exports variable is no longer needed.

Comments

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.