I'm working in typescript 1.5 in visual studio. I have a main class called app.ts, and another called FizzBuzzManager.ts. I can't figure out what is wrong with this code, but it outputs the error, "TypeError: jim.FizzBuzzManager is not a constructor".
app.ts
namespace jim { class Greeter { element: HTMLElement; span: HTMLElement; timerToken: number; constructor() { window.console.log("constructing Greeter."); this.init(); } private init() { window.console.log("Calling init."); var _fizzBuzzManager: any = new jim.FizzBuzzManager(); } } window.onload = () => { window.console.log("Hello") var greeter = new Greeter(); };
FizzBuzzManager.ts
namespace jim { export class FizzBuzzManager { constructor() { window.console.log("Making a FizzBuzzManager."); } public myThing: String = "Hi"; public fizzBuzz2() { window.console.log("fizzbuzzing2 " + this.myThing); } } export function fizzBuzz() { window.console.log("export function fizzbuzz"); } }
The output when looking at the compiled output in a browser is this:
Hello app.js:15:9 constructing Greeter. app.js:5:13 Calling init. app.js:9:13 TypeError: jim.FizzBuzzManager is not a constructor app.js:10:36
Classes in TypeScript do not require you to explicitly write a constructor. However if you are extending a base class you will need to create a constructor to call super() at a minimum.
The TypeScript docs have a great example of constructor usage: class Greeter { greeting: string; constructor(message: string) { this. greeting = message; } greet() { return "Hello, " + this. greeting; } } let greeter = new Greeter("world");
In TypeScript, the constructor method is always defined with the name "constructor". In the above example, the Employee class includes a constructor with the parameters empcode and name . In the constructor, members of the class can be accessed using this keyword e.g. this. empCode or this.name .
TypeScript defines a constructor using the constructor keyword. A constructor is a function and hence can be parameterized. The this keyword refers to the current instance of the class. Here, the parameter name and the name of the class's field are the same.
TypeError: jim.FizzBuzzManager is not a constructor
This is a common error when you use --out
: https://basarat.gitbook.io/typescript/main-1/outfile
You are responsible for loading the files in the right order. Don't use out and use external modules 🌹
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With