What does “… resolves to a non-module entity and cannot be imported using this construct” mean?

后端 未结 4 680
忘掉有多难
忘掉有多难 2020-11-28 04:05

I have some TypeScript files:

MyClass.ts

class MyClass {
  constructor() {
  }
}
export = MyClass;

MyFunc.ts

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 04:42

    TypeScript 2.7 introduces support by emitting new helper methods: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#support-for-import-d-from-cjs-form-commonjs-modules-with---esmoduleinterop

    So in tsconfig.json add these two settings:

    {
        // Enable support for importing CommonJS modules targeting es6 modules
        "esModuleInterop": true,
    
        // When using above interop will get missing default export error from type check since
        // modules use "export =" instead of "export default", enable this to ignore errors.
        "allowSyntheticDefaultImports": true
    }
    

    And now you can use:

    import MyClass from './MyClass';
    

提交回复
热议问题