简体   繁体   English

FlowType注释函数返回类型

[英]FlowType annotating function return type

I need to annotate in FlowType a function which return an object, I see I have several options: 我需要在FlowType中注释一个返回对象的函数,我看到我有几个选择:

A) Annotation on object exported and function A)关于对象导出和功能的注释

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

B) Annotation on function only: B)仅在功能上标注:

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

C) Annotation on object exported only: C)仅对导出的对象进行注释:

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

In my code I am using version A), but I would like to know if B or C could be equivalent and which version is suggestible and why. 在我的代码中,我正在使用版本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 } 

What I do is only annotate the function itself like 我所做的只是像注释函数本身一样

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

Because flow knows about const , it know the value can't change and will figure the type out itself. 因为flow知道const ,所以它知道值不会改变,并且会自行找出类型。

On the other hand if you'd use a let , then I'd annotate the variable itself also, so flow can check on reassigning the value if it has the correct type. 另一方面,如果您使用let ,那么我也会对变量本身进行注释,因此flow可以检查是否具有正确类型的值重新分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM