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

后端 未结 12 1258
清歌不尽
清歌不尽 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:28

    It might be helpful to think about it as if you were writing the code in JavaScript directly. I came across this question because I got the same error in an Angular 2 test spec written in TypeScript. After thinking about it based on the answers above, I realized that JavaScript would have no idea what my equivalent to your BuzzFeed class was because it was at the bottom of the file.

    I moved the class up to the top of the file before my first describe statement and everything works. Thought this might help others like myself.

    0 讨论(0)
  • 2021-01-17 08:32

    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

    0 讨论(0)
  • 2021-01-17 08:33

    When migrating from plain ES6 / Node.js using module.exports/require() it often happens to me that I forget to remove the old module.exports = <class> which also causes this error.

    0 讨论(0)
  • 2021-01-17 08:38

    In my case I had the problem when I used babel with preset-env to compile TS-sources.

    Works:

    {
        "presets": ["@babel/typescript"],
        "plugins": [
            "@babel/proposal-class-properties",
            "@babel/proposal-object-rest-spread",
            "@babel/plugin-transform-runtime"
        ],
        "ignore": ["node_modules", "src/test"]
    }
    

    Wrong:

    {
        "presets": [
            "@babel/typescript",
            [
                "@babel/preset-env",
                {
                    "targets": {
                        "browsers": [
                            "last 2 Chrome versions",
                            "last 1 Safari versions",
                            "last 1 Firefox versions"
                        ]
                    }
                }
            ]
        ],
        "plugins": [
            "@babel/proposal-class-properties",
            "@babel/proposal-object-rest-spread",
            "@babel/plugin-transform-runtime"
        ],
        "env": {
            "node": {
                "presets": [
                    [
                        "@babel/preset-env",
                        {
                            "targets": {
                                "esmodules": true,
                                "node": "current"
                            },
                            "modules": "auto"
                        }
                    ]
                ]
            }
        },
    
        "ignore": ["node_modules", "src/test"]
    }
    
    0 讨论(0)
  • 2021-01-17 08:38

    With JetBrains WebStorm, it's possible to get this error in your tests even if you have fixed a problem with imports in the code itself.

    e.g. the following fix:

    // import { Foo } from '@foo/bar';
    
    import Foo = require('@foo/bar);
    

    solved the issue, but it was necessary to run npm run compile or npm run test afterwards.

    Then running the tests in WebStorm worked as expected.

    0 讨论(0)
  • 2021-01-17 08:40

    at first, make sure that use

    /// <reference path = "FizzBuzzManager.ts" />   
    

    to refrence code to another .ts file at typescript root file

    to compile typescript files that exist in different file.ts use this command

    tsc --out app.js app.ts

    because this command converts all typescript files into app.js

    0 讨论(0)
提交回复
热议问题