1
Debugger listening on port 5858
c:\workspace\Projects\test\test\Car.js:16
})(Vehicle);
   ^
ReferenceError: Vehicle is not defined
    at Object.<anonymous> (c:\workspace\test\test\Car.js:16:4)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.runMain [as _onTimeout] (module.js:501:10)
    at Timer.listOnTimeout (timers.js:119:15)

I am using Visual Studio 2013 with typescript plugin. I got this error and I tried import and export but that didn't work neither. How can I fix it?

source:

Car.ts

class Car extends Vehicle {
  public talk() { console.log ("vehicle"); }
}
var rx_car = new Car("");
rx_car.say(); 

Vehicle.ts

class Vehicle {
  private _name: string;
  constructor(name: string) { this._name = name; }
  public say (subject = "name is") { console.log(subject + " " + this._name); }
  get name() { return this._name; }
  set name(name: string) {this._name = name; }
}

compiled file:

Car.js

var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
var Car = (function (_super) {
    __extends(Car, _super);
    function Car() {
        _super.apply(this, arguments);
    }
    Car.prototype.talk = function () {
        console.log("vehicle");
    };
    return Car;
})(Vehicle);
var rx_car = new Car("");
rx_car.say();
//# sourceMappingURL=Car.js.map

Car.js.map

{"version":3,"file":"Car.js","sourceRoot":"","sources":["Car.ts"],"names":["Car","Car.constructor","Car.talk"],"mappings":";;;;;;AAAA,IAAM,GAAG;IAASA,UAAZA,GAAGA,UAAgBA;IAAzBA,SAAMA,GAAGA;QAASC,8BAAOA;IAEzBA,CAACA;IADQD,kBAAIA,GAAXA;QAAgBE,OAAOA,CAACA,GAAGA,CAAEA,SAASA,CAACA,CAACA;IAACA,CAACA;IAC5CF,UAACA;AAADA,CAACA,AAFD,EAAkB,OAAO,EAExB;AACD,IAAI,MAAM,GAAG,IAAI,GAAG,CAAC,EAAE,CAAC,CAAC;AACzB,MAAM,CAAC,GAAG,EAAE,CAAC"}

Vehicle.js

var Vehicle = (function () {
    function Vehicle(name) {
        this._name = name;
    }
    Vehicle.prototype.say = function (subject) {
        if (subject === void 0) { subject = "name is"; }
        console.log(subject + " " + this._name);
    };
    Object.defineProperty(Vehicle.prototype, "name", {
        get: function () {
            return this._name;
        },
        set: function (name) {
            this._name = name;
        },
        enumerable: true,
        configurable: true
    });
    return Vehicle;
})();
//# sourceMappingURL=Vehicle.js.map

Vehicle.js.map

{"version":3,"file":"Vehicle.js","sourceRoot":"","sources":["Vehicle.ts"],"names":["Vehicle","Vehicle.constructor","Vehicle.say","Vehicle.name"],"mappings":"AAAA,IAAM,OAAO;IAEXA,SAFIA,OAAOA,CAECA,IAAYA;QAAIC,IAAIA,CAACA,KAAKA,GAAGA,IAAIA,CAACA;IAACA,CAACA;IACzCD,qBAAGA,GAAVA,UAAYA,OAAmBA;QAAnBE,uBAAmBA,GAAnBA,mBAAmBA;QAAIA,OAAOA,CAACA,GAAGA,CAACA,OAAOA,GAAGA,GAAGA,GAAGA,IAAIA,CAACA,KAAKA,CAACA,CAACA;IAACA,CAACA;IAC7EF,sBAAIA,yBAAIA;aAARA;YAAaG,MAAMA,CAACA,IAAIA,CAACA,KAAKA,CAACA;QAACA,CAACA;aACjCH,UAASA,IAAYA;YAAGG,IAAIA,CAACA,KAAKA,GAAGA,IAAIA,CAACA;QAACA,CAACA;;;OADXH;IAEnCA,cAACA;AAADA,CAACA,AAND,IAMC"}
3
  • Is this running in node.js? Commented Jul 29, 2015 at 2:49
  • yes. I am using version 0.12.7 Commented Jul 29, 2015 at 2:53
  • @RyanCavanaugh thanks for asking that clarification Commented Jul 29, 2015 at 4:30

1 Answer 1

4

Since you are running on node please use import/require aka external modules

i.e. export Vehicle combined with import {Vehicle} from "./Vehicle" and compile with --module commonjs

More : http://basarat.gitbooks.io/typescript/content/docs/project/external-modules.html

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.