2017-10-09 70 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();