代码之家  ›  专栏  ›  技术社区  ›  Hemadri Dasari

TypeScript:TypeError:应用程序不是构造函数

  •  0
  • Hemadri Dasari  · 技术社区  · 7 年前

    应用程序ts

    import { EventEmitter } from 'events';
    
    interface Emitter extends EventEmitter {
    
    }
    class App {
       constructor(protected app: Emitter){}
    }
    
    export default = App;
    

    我正在生成App.ts以 App.js tsc 命令。文件在dist文件夹中生成为

    我还有另一个文件Test.js

    TypeError:应用程序不是构造函数

    const App = require("./dist/App);
    
    module.exports = function(app){
        const appInstance = new App(app)
    }
    

    我是否在App.ts文件中做错了什么,这就是为什么我会出现上述错误?

    如何解决此问题?

    2 回复  |  直到 7 年前
        1
  •  4
  •   felixmosh    7 年前

    Typescript编译 export default = App; .

    你可以用两种方法来解决这个问题:

    1. 将js文件中的require更改为: const App = require("./dist/App).default;
    2. 添加到您的 tsconfig.json 文件在 compilerOptions allowSyntheticDefaultImports: false .
        2
  •  3
  •   Artyom Amiryan    7 年前

    你以错误的方式导出了应用程序,请这样导出

    export default class App {
       constructor(protected app: App){}
    }
    
        3
  •  2
  •   Paul LeBeau    6 年前

    例如。 class MyThing {} 在一个名为 MyThing.ts

    将文件重命名为 mything.ts 解决了问题。

    推荐文章