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

后端 未结 12 1260
清歌不尽
清歌不尽 2021-01-17 07:55

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,

12条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-17 08:46

    I tried to repeat your problem and I did not find any error:

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

    Then

    c:\Work\TypeScript-playground>node_modules\.bin\tsc --out app.js app.ts FizzBuzzManager.ts
    

    and compiled app.js file looks like this:

    var jim;
    (function (jim) {
        var Greeter = (function () {
            function Greeter() {
                window.console.log("constructing Greeter.");
                this.init();
            }
            Greeter.prototype.init = function () {
                window.console.log("Calling init.");
                var _fizzBuzzManager = new jim.FizzBuzzManager();
            };
            return Greeter;
        })();
        window.onload = function () {
            window.console.log("Hello");
            var greeter = new Greeter();
        };
    })(jim || (jim = {}));
    var jim;
    (function (jim) {
        var FizzBuzzManager = (function () {
            function FizzBuzzManager() {
                this.myThing = "Hi";
                window.console.log("Making a FizzBuzzManager.");
            }
            FizzBuzzManager.prototype.fizzBuzz2 = function () {
                window.console.log("fizzbuzzing2 " + this.myThing);
            };
            return FizzBuzzManager;
        })();
        jim.FizzBuzzManager = FizzBuzzManager;
        function fizzBuzz() {
            window.console.log("export function fizzbuzz");
        }
        jim.fizzBuzz = fizzBuzz;
    })(jim || (jim = {}));
    

    Chrome browser reports in its console:

    app.js:15 Hello
    app.js:5 constructing Greeter.
    app.js:9 Calling init.
    app.js:24 Making a FizzBuzzManager.
    

    There is a good explanation of the error you are getting here: Javascript: TypeError: ... is not a constructor (not that it reveals the origin of the problem but you may see the problem in your transpiled code.)

提交回复
热议问题