TypeScript / JavaScript - 导入所有类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36293160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript / JavaScript - import all types
提问by Ondra ?i?ka
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
.
假设我有myClass.ts
和otherClass.ts
。我想从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.
第一个需要我列出所有内容。我想导入所有类型。
第二种语法需要命名空间前缀:
foo.ClassA
.我知道最后一个是 TypeScript 1.4,但仍受支持。
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 documentationdoesn't hint anything such.
该文档没有暗示任何此类内容。
采纳答案by David Sherret
With ES6 modules, the closest thing available to what you want is a namespace import:
使用 ES6 模块,最接近您想要的东西是命名空间导入:
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.
您可以在import
文档中查看可用的导入类型。
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.
那是为了导入命名导出。您可以在我参考的文档或我的回答中阅读相关内容。
回答by user
You can use triple slashes import:
您可以使用三重斜线导入:
/// <reference path="./actionsCollection.ts" />
They must have to be the on the first line(s) of the file.
它们必须位于文件的第一行。