85

How can I import all types from certain file?

Let's say I have myClass.ts and otherClass.ts. I want to import all classes from otherClass.ts.

I've seen few syntaxes for imports.

import ClassA, { ClassB, ClassC } from 'otherClass';

import * as foo from 'otherClass';

import foo = require('otherClass');

import 'rxjs/Rx';
  1. The first needs me to list everything. I'd like to import all types.

  2. The second syntax needs the namespace prefix: foo.ClassA.

  3. I understand that the last one is TypeScript 1.4, but still supported.

Is there something like the following?

import * from "otherClass";
...
   var x = new ClassA()

Also, what's the meaning of the { ... } and some of the types being outside and some inside?

The documentation doesn't hint anything such.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277

2 Answers2

84

With ES6 modules, the closest thing available to what you want is a namespace import:

import * as foo from './otherClass';

The use it individual exports as

foo.ClassA

You can see the available kinds of imports in the import documentation.

Also, what's the meaning of the { ... } and some of the types being outside and some inside?

That's for importing named exports. You can read about that in the documentation I referenced or in my answer here.

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
David Sherret
  • 101,669
  • 28
  • 188
  • 178
  • 1
    But this doesn't work for types or interfaces, does it? I would be very much interested on an equivalent way of handling those. – DanielM Oct 28 '17 at 16:55
  • 1
    @DanielM yes it does. You just need to ensure the types and interfaces are exported from the file being imported. – David Sherret Oct 28 '17 at 17:01
  • In the tests I did this was not working -- the compiler complains if I do `foo.SomeExportedType`. – DanielM Oct 28 '17 at 17:09
  • @DanielM make sure you're using the type or interface in the place a type or interface is supposed to be used. I use namespace imports all the time with types and interfaces. If it's still not working, I would recommend asking a new question or doing a deeper investigation into why it's not working. – David Sherret Oct 28 '17 at 17:15
  • 1
    Awesome, thank you! Deeper research will not be needed, it was simply that my tests were too simple -- I was typing "foo." in the middle of nowhere and expecting VS Code to suggest me the types; and then tried explicitly typing "foo.SomeType" and I got an error, of course. When properly placed, it works like a charm :) – DanielM Oct 28 '17 at 17:21
  • 1
    Hello , is there a way import all interface without alias . For example like this ; import * from './index' – captainblack May 31 '19 at 07:34
  • @user3820266 nope. – David Sherret May 31 '19 at 13:46
  • 1
    @DavidSherret Is there the concept of importing the namespace like in CSharp/VB .NET where I don't have spell Foo.Method() just Method() or reference an enum imported via ```import * as Page1Models from './Page1Models/``` it's quite a nuisance to be so verbose when in the correct context. – Meryan Feb 28 '20 at 23:01
3

You can use triple slashes import:

/// <reference path="./actionsCollection.ts" />

They must have to be the on the first line(s) of the file.

  1. When do I need a triple slash reference?
  2. https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144