`type`在以下JavaScript代码段中的含义是什么?
我是(全部)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
你指的是使用流类型库,你可以阅读更多关于它在这里的文章:https://flow.org/
好的,我明白了。看看这个: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
下面的文章中的代码片段段落解释了什么是'进口type'是为了。 – 4castle
Aaaargh,明白了!这就是为什么我在代码中感到困惑,因为我没有从JS重命名。如果您将答复转到答案部分,我会接受它作为答案。 – swdev
Altough,@ 4castle,虽然有一个问题。 'fromJS'是函数,对不对?这是否意味着'type'会返回一个函数作为'import type {fromJS}'的结果?或者,返回变量类型'fromJS'的类型是? – swdev