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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:53:00  来源:igfitidea点击:

TypeScript / JavaScript - import all types

javascripttypescriptimportecmascript-6

提问by Ondra ?i?ka

How can I import all types from certain file?

如何从某个文件导入所有类型?

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

假设我有myClass.tsotherClass.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';
  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.

  1. 第一个需要我列出所有内容。我想导入所有类型。

  2. 第二种语法需要命名空间前缀:foo.ClassA.

  3. 我知道最后一个是 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 importdocumentation.

您可以在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.

它们必须位于文件的第一行。

  1. When do I need a triple slash reference?
  2. https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html
  1. 我什么时候需要三斜杠参考?
  2. https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html