2017-09-26 40 views
0

我需要在流動型註釋其返回一個對象的函數,我看我有幾種選擇:流動型標註函數返回類型

A)詮釋對象導出和功能

const getForecastHourly:ActionType = (query:number):ActionType => ... 

B)註釋僅在功能:

const getForecastHourly = (query:number):ActionType => ... 

C)上對象註釋僅導出:

const getForecastHourly:ActionType = (query:number) => ... 

在我的代碼中,我使用的是版本A),但是我想知道B或C是否可以等價,哪個版本是可以理解的以及爲什麼。

// @flow 
 
import {ActionType} from '../../types' 
 

 
import 'isomorphic-fetch' 
 
import * as api from '../../app/api' 
 
import * as types from './forecastHourlyActionTypes' 
 

 
const getForecastHourly:ActionType = (query:number):ActionType => ({ 
 
    type: types.GET_FORECAST_HOURLY, 
 
    payload: new Promise((resolve, reject) => { 
 
    fetch(api.forecast(query)).then(response => { 
 
     resolve(response.json()) 
 
    }) 
 
    }) 
 
}) 
 

 
const setForecastHourlyActiveReportType:ActionType = (type:string):ActionType => ({ 
 
    type: types.SET_FORECAST_HOURLY_ACTIVE_REPORT_TYPE, 
 
    payload: type 
 
}) 
 

 
export { getForecastHourly, setForecastHourlyActiveReportType }

export type ActionType ={ 
 
    +type:string, 
 
    +payload: Object 
 
    }

回答

1

我做的是僅標註功能本身就像

const getForecastHourly = (query: number): ActionType => ({ /* - */ }); 

因爲flow知道const,它知道價值無法改變,並將自己的類型。

另一方面,如果您使用的是let,那麼我也會註釋變量本身,因此flow可以檢查重新賦值,如果它具有正確的類型。