Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript: import default export from module

I have the following definition type file:

// index.d.ts
declare module 'Transformer' {
  class EditableElement {
      constructor(target: SVGPoint);
  }
  export = EditableElement;
}

And I want to import EditableElement. But when I write the following line:

import {EditableElement} from 'Transformer';

I get the next error:

Module "Transformer" resolves to a non-module entity and cannot be imported using this construct.

How could I import the EditableElement class? Actually, I just want to make use of that class. I don't want the import directive to have a collateral effect in my code. I just want use it :'(

like image 548
Cequiel Avatar asked Apr 25 '17 20:04

Cequiel


Video Answer


1 Answers

This falls into ES6 / CommonJS interop.

My recommendation is to not relying on interop and use the old syntax instead:

const EditableElement = require('Transformer')

If you NEED to target es6/es2015, then you can do:

import * as EditableElement from 'Transformer'

// with `allowSyntheticDefaultImports`
import EditableElement from 'Transformer'

UPDATE: with [email protected] released, you can now do import EditableElement from 'Transformer' directly.

Turn on esModuleInterop in your tsconfig.json

like image 177
unional Avatar answered Oct 14 '22 16:10

unional