admin管理员组

文章数量:1394977

This typescript:

export enum UID {
    FACTORY,
    ROBOT
}

piles 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 piler to know what it's doing. Unfortunately the javascript can't be executed. nodejs plains 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 piler. 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:

UPDATE 2: for those of you that experience the same error. You can insert the missing semicolons manually but that is not a very fortable solution since you have to do this after every pilation. 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 piler

This typescript:

export enum UID {
    FACTORY,
    ROBOT
}

piles 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 piler to know what it's doing. Unfortunately the javascript can't be executed. nodejs plains 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 piler. 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./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 fortable solution since you have to do this after every pilation. 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 piler

Share Improve this question edited Sep 13, 2024 at 10:20 Jonas 129k102 gold badges327 silver badges405 bronze badges asked Mar 6, 2013 at 16:54 lhklhk 30.3k36 gold badges137 silver badges220 bronze badges 10
  • 2 Is that the whole script? – Bergi 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/antisanity/cRNeN – canon Commented Mar 6, 2013 at 16:59
  • 1 incorrect concatenation with another file? – John Dvorak 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. – Pointy Commented Mar 6, 2013 at 17:02
  • 1 And it's not needed because you're in node.js so exports is already defined. – Matt Browne Commented Mar 6, 2013 at 17:29
 |  Show 5 more ments

3 Answers 3

Reset to default 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 piled 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./workitem/364

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;

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.

本文标签: nodejsTypescript generated JavaScript has a function that is not a functionStack Overflow