typescript: import default export from module

typescript export default
typescript export const
typescript export function
typescript export multiple functions
typescript export object
typescript import alias
typescript export type
typescript import all

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

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 `allowSyntheticDefaultImport`
import EditableElement from 'Transformer'

UPDATE: with TypeScript@2.7 released, you can now do import EditableElement from 'Transformer' directly.

Turn on esModuleInterop in your tsconfig.json

Import a module for side-effects only, This falls into ES6 / CommonJS interop. My recommendation is to not relying on interop and use the old syntax instead: const EditableElement  Default exports are marked with the keyword default; and there can only be one default export per module. default exports are imported using a different import form. default exports are really handy. For instance, a library like jQuery might have a default export of jQuery or $, which we’d probably also import under the name $ or jQuery. JQuery.d.ts #

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';

typescript: import default export from module, I have tried importing fs module using import fs from 'fs' but this gives an error: `​Module [ts] Module 'xxx' has no default export. xcatliu/typescript-tutorial#19. Export and Import Statements: Example 7. There are two types of exports: default exports and named exports. Each module can have at most one default export in a module, but it can have multiple

Do you have reference path like that?

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

Importing default module fails when using ES6 syntax · Issue #3337 , Since you can already get the desired effect with TypeScript's existing export = , and since you can import that with the existing import x = require(. A TypeScript module can say export default myFunction to export just one thing. Use import myFunction from "./myModule" to bring it in. More commonly, TypeScript modules say export myFunction in which case myFunction will be one of the properties on the exported object. Use import { myFunction } from "./myModule" to bring it in.

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';

ES6 Modules default exports interop with CommonJS · Issue #2719 , You would import it (in bar.ts ) using ES6 syntax as follows: import Foo You cannot explore a module with intellisense to see if it has a default export or not. export class Greeter {} which will compile to. exports.Greeter = Greeter; But what I really want is this: exports = Greeter; So that I can use it like this: import { Greeter } from "greeter"; const greeter = new Greeter(); and not. import { Greeter } from "greeter"; const greeter = new Greeter.Greeter(); Is this possible with Typescript?

Avoid Export Default - TypeScript Deep Dive, TypeScript has also adopted ES6 modules. There are more things to export and import, such as interfaces, types, namespaces, and enums. Each module can have at most one default export in a module, but it can have  I suggest making the existing export = syntax compatible with export default. Right now it doesn't seem possible to perform a default import of modules declared with the old syntax. Take the tape module for example. I can't import it using es6 syntax and the existing definition file. The following directive:

A Handy Guide to Export and Import Modules for JavaScript and , So let's use js object with a default key for the default export! module.exports = { foo: 1, bar: 2, default: () => {} }const module  For anyone not wanting to deal with default you re-export an imported module like so import {A} from './A.js'; import {B} from './B.js'; const C = 'some variable' export {A, B, C} This way you can export all the variables you either imported or declared in this file.

Great import schism: Typescript confusion around imports explained, can be exported from module to be imported in other module. Typescript offer two export types: named and default. Named export // adams.ts export function  This is implemented by exposing a named export with a special name 'default', so you could also do the following: import { default as shortestPath } from './shortestPath'; If you need this to import a module that has both a default export and named exports, then this module might not have one single responsibility.