2017-10-09 74 views
0

我遇到了这个源代码。 我不明白的第一行:在ES6中进行导入

import type { a, b, c, d } from 'types'

有什么用

import { a, b, c, d } from 'types' 的差别,你能解释一下?由于

import type { a, b, c, d } from 'types'// not sure what it does 
 
import { a, b, c, d } from 'types' // I am familar with this

+3

单词'type'后面是否有逗号? –

+0

不,没有逗号,代码按预期工作,逗号的缺失实际上令我困惑 – Radex

+2

代码使用流式 – Radex

回答

3

这不是香草的JavaScript import使用。这可能是Flow,或者是一个密切相关的传译语言。

我在Flow项目中发现了一篇名为Announcing Import Type的博客文章。我不知道Flow,但它看起来像是一个严格类型的JavaScript超集。 import type声明是您如何导入有关某个类的类型信息,而无需导入该类本身。他们给出了一个例子,你可能想要在函数中声明动态类型的形式参数,并且需要导入适当的类型:

import type {Crayon, Marker} from 'WritingUtensils'; 
module.exports = function junkDrawer(x: Crayon, y: Marker): void {} 
-2

从MDN,虽然从OP缺少的昏迷是怪异:

导入默认值:它可以有一个默认的出口(无论 是一个对象,功能,班级等)。导入声明可能会使用 导入此类默认值。

最简单的版本直接导入默认:

import myDefault from '/modules/my-module.js';

它也可以 使用它与上面看到的那些空间(namespace进口或 命名进口)的默认语法。在这种情况下,默认导入必须首先声明为 。例如:

import myDefault, {foo, bar} from '/modules/my-module.js'; // specific, named imports

换句话说,这将导入默认为myDefault,然后导入指定的出口。

1

它从文件导入类型定义。

// Here you are importing the actual method, variable from the file. 
import xyz from 'abc';` 

// Here you are importing the type defination of xyz 
import type { xyz } from 'abc'; 

现在,如果你想使用它作为

let a: xyz = new xyz();