TypeScript: TypeError: App is not a constructor
webpack_imported_module is not a constructor
ctor is not a constructor react-native
is not a constructor angular 6
typescript default is not a constructor
typeerror: user is not a constructor
typeerror graphqlobjecttype is not a constructor
javascript typeerror default is not a constructor
I just started with typescript and trying to create an instance to the typescript class but I am unsuccessful.
Below are my files
App.ts
import { EventEmitter } from 'events'; interface Emitter extends EventEmitter { } class App { constructor(protected app: Emitter){} } export default = App;
I am generating App.ts to App.js
file using tsc
command. The file got generated in dist folder as App.js
I have other file called Test.js
In Test.js I am importing generated App.js file and created an instance to it like below but I get below error
TypeError: App is not a constructor
Test.js
const App = require("./dist/App); module.exports = function(app){ const appInstance = new App(app) }
I don't understand why this is throwing such error though I have constructor available in App.ts file.
Am I doing something wrong in App.ts file and that's why I get the above error?
How can I resolve this issue?
Typescript compiles export default = App;
as an export of an Object with a property default.
You can solve that with 2 methods:
- Change your require in the js file to that:
const App = require("./dist/App).default;
- Add to your
tsconfig.json
file in the compilerOptionsallowSyntheticDefaultImports: false
.
TypeError: "x" is not a constructor, function known as Object() is used to create and initialize an object. In Promises. When returning an immediately-resolved or immediately-rejected Promise, you do not need to create a new Promise () and act on it. This is not legal (the Promise constructor is not being called correctly) and will throw a TypeError: this is not a constructor exception: return new Promise.resolve(true);
you have exported App by wrong way, export it like so
export default class App { constructor(protected app: App){} }
Strange JavaScript Errors and How to Fix Them, The JavaScript exception "is not a constructor" occurs when there was an attempt to use an object or a variable as a constructor, but that object rottenoats changed the title Please add support for Typescript TypeError: undefined is not a constructor for Typescript on Jul 7, 2017. the-specialist mentioned this issue on Jul 12, 2017.
In my case it was because the file had the same name as the class.
Ie. class MyThing {}
was defined in a file called MyThing.ts
.
Renaming the file to mything.ts
solved the problem.
Hope this helps someone.
JavaScript, As the name suggests a "x" Is Not a Constructor TypeError is thrown when incorrectly trying to invoke the constructor of a variable or object that doesn't actually have a constructor itself. Without more specific information it's not possible to to make a diagnosis but it is worth noting that Angular 2’s AOT compiler does not have the same semantics as TypeScript in a significant number of cases. It outputs TypeScript which you did not write which is then in turn compiled by TypeScript. This causes errors.
I just started with typescript and trying to create an instance to the typescript class but I am unsuccessful. Below are my files App.ts import { EventEmitter } from I am running the following typescript code in the ES6 target environment and it says that "Cars is not a constructor" I have followed the link and tried changing the target environment to ES5. It is working fine. Can some one tell why it is not working for target ES6. Here is my TypeScript code:
file I'm getting: "main.js:5 Uncaught TypeError: Counter is not a constructor" More info on vueify: https://vuejs.org/guide/application.html. To dig into the "x" Is Not a Constructor TypeError we should first refresh ourselves on how constructors work in JavaScript. A constructor is merely a special method that is automatically added to every Object (or derived type therein) that, when called, actually performs the instantiation and overall creation of the object in question.
Expected Behavior Expected the Typescript project to resolve the import statement and not have the app crash during runtime. Will compiles GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together. Object is not a constructor
Comments
- Shouldn't the interface and the class be declared with different names? They are both called
App
. You can try declaring the interface asIApp
. - @ConnorsFan Sorry my bad it was typo error. I updated my question
- Awesome the first option resolved the issue but the second didn't work. You saved lot of time. Thank you :)
- But this is still valid right class App{} export default App; Can you please explain why this is not valid in typescript
- I tried with your solution unfortunately I still get that error
- because you can't set
export default
to equal smth., because there isn't syntax like it in typescript, instead you should addexport default
beforeclass, variable,function and so on
to export it, regarding error where is located yourTest.js
file? I think you wrote App.js path wrong