Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Does This Typescript Output "[Class] is not a constructor."?

Tags:

typescript

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 
like image 737
Jim Avatar asked Jan 03 '16 21:01

Jim


People also ask

Does a TypeScript class need a constructor?

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.

How do I create a constructor in TypeScript class?

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");

How do you call a constructor from a class in TypeScript?

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 .

How do you define a constructor in TypeScript?

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.


1 Answers

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 🌹

like image 172
basarat Avatar answered Oct 21 '22 20:10

basarat