2017-04-22 72 views
0

我是(全部)JavaScript中的新手(或者確切地說,在ES6中),並使用this article來指導我。我想問的是第二行。 type在那個import語句中意味着什麼?是type在JavaScript中的關鍵字?因爲如果正確理解它,我知道該行:import type { fromJS } from 'immutable',意思是從包immutable(我來自Python背景)導入fromJS函數。`type`在以下JavaScript代碼段中的含義是什麼?

我也看到在action: {type: string, payload: any }) => {行中有type參數。但我想這只是巧合吧?

import Immutable from 'immutable' 
import type { fromJS } from 'immutable' 

import { SAY_HELLO } from '../action/hello' 

const initialState = Immutable.fromJS({ 
    message: 'Initial message', 
}) 

const helloReducer = (state: fromJS = initialState, action: {type: string, payload: any }) => { 
    switch(action.type) { 
    case SAY_HELLO: 
     return state.set('message', action.payload) 
    default: 
     return state 
    } 
} 

export default helloReducer 

DD

+2

下面的文章中的代碼片段段落解釋了什麼是'進口type'是爲了。 – 4castle

+0

Aaaargh,明白了!這就是爲什麼我在代碼中感到困惑,因爲我沒有從JS重命名。如果您將答覆轉到答案部分,我會接受它作爲答案。 – swdev

+0

Altough,@ 4castle,雖然有一個問題。 'fromJS'是函數,對不對?這是否意味着'type'會返回一個函數作爲'import type {fromJS}'的結果?或者,返回變量類型'fromJS'的類型是? – swdev

回答

1

你指的是使用流類型庫,你可以閱讀更多關於它在這裏的文章:https://flow.org/

+1

好的,我明白了。看看這個:https://flow.org/en/docs/types/modules/#importing-and-exporting-types-a-classtoc-idtoc-importing-and-exporting-types-hreftoc-importing-and-exporting -typesa我看到'import type'的用法。通過這個https://flow.org/en/docs/types/,我明白它會導入變量的數據類型或函數結果。 Cmiiw – swdev

相關問題