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,
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.
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
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.
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"]
}
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.
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