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';
The first needs me to list everything. I'd like to import all types.
The second syntax needs the namespace prefix: foo.ClassA
.
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.
The allowJs setting allows JavaScript files to be imported inside your TypeScript files. The setting basically allows JavaScript and TypeScript files to live in the same project.
Yes, you can use it in a same manner that you would use it in Javascript. Typescript is superset of Javascript, all things possible in Javascript are also possible in Typescript.
Use a named export to export a type in TypeScript, e.g. export type Person = {} . The exported type can be imported by using a named import as import {Person} from './another-file' . You can have as many named exports as necessary in a single file.
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.
You can use triple slashes import:
/// <reference path="./actionsCollection.ts" />
They must have to be the on the first line(s) of the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With