TypeError: editorjs_1.default is not a constructor
I am getting the same error as #752 when importing editorjs in TypeScript
TypeError: editorjs_1.default is not a constructor
Failing index.ts
import EditorJS from "@editorjs/editorjs";
class EditorClass {
private editor: any;
constructor() {
this.editor = new EditorJS();
}
}
var Editor: any = new EditorClass();
The TypeScript line this.editor = new EditorJS(); transpiles to different javascript, depending on how editorjs is imported:
1. const-require: Works as expected
const EditorJS = require("@editorjs/editorjs");
...
this.editor = new EditorJS();
2. module-import Error at runtime
import EditorJS from "@editorjs/editorjs";
...
this.editor = new editorjs_1.default();
3. import-require fails to transpile
import EditorJS = require("@editorjs/editorjs");
Produces the error
src/index_import-require.ts:7:19 - error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
7 this.editor = new EditorJS();
I would like to follow best practice for TypeScript imports , version (2) or (3), but both methods cause errors. I have to bypass the tslint rule no-var-requires to use (1).
To reproduce:
# clone repo branch
git clone --depth 1 https://github.com/machuu/writing-app.git -b reproduce-default-not-a-constructor
cd writing-app
# Start docker container
docker-compose up -d
# get into container shell, if you want to debug/troubleshoot
docker-compose exec -it writing-app_dev bash
Open in browser: http://localhost:8000
@machuu enabling "esModuleInterop": true in your tsconfig.json fixes the problem.
After setting "esModuleInterop": true, I'm still getting errors
import EditorJS from "@editorjs/editorjs"
gives me the same runtime error
TypeError: editorjs_1.default is not a constructor
import { EditorJS } from "@editorjs/editorjs"
gives the error
TypeScript error: src/editor.ts(4,10): Error TS2305: Module '"node_modules/@editorjs/editorjs/types/index"' has no exported member 'EditorJS'.
import EditorJS = require("@editorjs/editorjs");
gives the error
TypeScript error: src/editor.ts(49,19): Error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
@machuu it's import EditorJS from "@editorjs/editorjs" — without braces { } (default import). I tested it in your repository and it works.
@mmiszy Awesome, it's working now. Thanks for the help
@machuu enabling
"esModuleInterop": truein yourtsconfig.jsonfixes the problem.
I was sarching from few days ago from this issue, the problem was third-parties library. So thanks very much!