26

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 :'(

2
  • Look at this question stackoverflow.com/questions/13407036/… Commented Apr 25, 2017 at 20:33
  • 1
    This is not "default export", it is CommonJS format. Commented Apr 25, 2017 at 23:05

4 Answers 4

38

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

Sign up to request clarification or add additional context in comments.

1 Comment

esModuleInterop saved my day
1

declare module is deprecated to use in your own typescript modules.You have to use either export or export default.

export class EditableElement {
      constructor(target: SVGPoint);
  }

For import you can use either import {EditableElement} from 'Transformer'; or import * as EditableElement from 'Transformer';

2 Comments

Thanks. This is automatically added by the "typings" command line: typings install file:/path/to/my/package/Transformer.d.ts
Oh. I missed that it was d.ts. Maybe i was wrong. I prefer to use .ts and .tsx for declaring classes.
1

Do you have reference path like that?

 /// <reference path="*/**/myModules.d.ts" />
 import * as m from "SomeModule";

Comments

0

I like to think of this in the following way.

Scenario 1 - Export / import explicit things (Not a default export. This results in the need for wrapping your import name with '{}').

// Export (fileX)
export const something = 'hello world';

// Import (fileY)
import { something } from 'fileX';

Scenario 2 - Export / import a default (This results in no need for wrapping your import name with '{}'). The name that you choose, 'something' below, would be the alias for accessing your import.

// Export (fileX)
export default 'hello world';

// Import (fileY)
import something from 'fileY';

1 Comment

In scenario 1, the entity that I'm exporting and importing is a class. When I try to use the imported class, I get _MyExportFile.default is not a constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.