Reputation: 61
I want to debug my project and I find small problem with creating instance of class. I try to describe it on simple example.
//this is saved in file Engine.ts
module Car {
export class Engine {
name: string;
constructor(name: string) {
this.name = name;
}
getName(): string{
return this.name;
}
}
}
This class describes simple engine with his name. Now I want to create Engine in some vehicle:
///<reference path="Engine.ts"/>
//this is saved in file app.ts
module Car {
export class Vehicle {
name: string;
constructor(name: string) {
this.name = name;
}
buildCar() : string {
var engine = new Engine("Volkswagen 1.9TDI");
return "Name of the vehicle is " + this.name + " and has engine " + engine.getName();
}
}
}
window.onload = () => {
var car = new Car.Vehicle("Skoda Rapid");
alert(car.buildCar);
}
The problem is with creating instance of class Engine. The browser console returns error that Car.Engine is not a constructor. How can I fix this problem? I have more difficult project and this describes only main principle of problem. In my project I must create instance of some class in function of other class.
Upvotes: 3
Views: 3889
Reputation: 250842
As well as sometimes being related to not including the script at runtime, as Ryan answered (definitely the case for this specific question), this can also be caused by the ordering of classes within a file.
Moving the class mentioned in the error to a position before it is used will solve the problem if this is the problem you have.
Upvotes: 1
Reputation: 276259
Compile app.ts with the --out compiler flag. This will include the code for engine.ts in the generated file.
Upvotes: 0
Reputation: 220944
It sounds like you've compiled to engine.js and app.js, but have not actually loaded engine.js through a script
tag like you have for app.js.
Upvotes: 7