在ES6中取消导入

时间:2017-10-09 11:36:35

标签: javascript ecmascript-6 flowtype

我遇到了这个源代码。 我不明白第一行:

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 个答案:

答案 0 :(得分:3)

这不是vanilla JavaScript import用法。这可能是Flow,或者是一种密切相关的转换语言。

我在Flow项目中发现了一篇名为Announcing Import Type的博客文章。我不知道Flow,但它看起来像JavaScript的严格类型超集。 import type语句是在不导入类本身的情况下导入类的类型信息的方法。他们给出了一个示例,您可能希望在函数中声明stirctly类型的形式参数,并需要导入适当的类型:

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

答案 1 :(得分: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();

答案 2 :(得分:-2)

来自MDN,虽然OP缺少的昏迷很奇怪:

  

导入默认值:可以进行默认导出(无论是否导出)   是一个对象,一个函数,一个类等。进口声明可以   然后用于导入此类默认值。

     

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

     

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

     

也有可能   使用上面看到的默认语法(名称空间导入或   命名进口)。在这种情况下,必须进行默认导入   首先宣布。例如:

     

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

换句话说,这会将默认值导入为myDefault,然后导入命名的导出。