A 缺少类型注释。A 是在函数类型中声明的类型参数

Posted

技术标签:

【中文标题】A 缺少类型注释。A 是在函数类型中声明的类型参数【英文标题】:Missing type annotation for A. A is a type parameter declared in function type 【发布时间】:2019-07-05 19:19:42 【问题描述】:

我有以下代码:

import  combineReducers  from 'redux';
import planeReducer from './plane/reducer';

export default combineReducers(
    planes: planeReducer
);

运行时正确运行:

> expo start

然后,当我使用以下命令运行flow 时:

>  npm run flow

我收到以下流程错误:

Missing type annotation for A. A is a type parameter declared in function type [1] and was implicitly instantiated at
call of combineReducers [2].

     src/store/index.js
      1| import  combineReducers  from 'redux';
      2| import planeReducer from './plane/reducer';
      3|
 [2]  4| export default combineReducers(
      5|        planes: planeReducer
      6| );
      7|

     flow-typed/npm/redux_v4.x.x.js
 [1] 56|   declare export function combineReducers<O: Object, A>(reducers: O): CombinedReducer<$ObjMap<O, <S>(r: Reducer<S, any>) => S>, A>;

然后,当我修改上面的代码时,添加:&lt;any, any&gt; 如下:

import  combineReducers  from 'redux';
import planeReducer from './plane/reducer';

export default combineReducers<any, any>(
    planes: planeReducer
);

当我像以前一样再次运行flow 时,flow 错误消失了,但如果我再次运行:

> expo start

我收到以下运行时错误:

[01:30:16] Your app is running at exp://192.168.1.194:19000
Logs for your project will appear below. Press Ctrl+C to exit.
[01:30:16] SyntaxError: D:\react-navigation-header-issue\src\store\index.js: Unexpected token, expected ";" (4:34)
[01:30:16]   2 | import planeReducer from './plane/reducer';
[01:30:16]   3 |
[01:30:16] > 4 | export default combineReducers<any, any>(
[01:30:16]     |                                   ^
[01:30:16]   5 |        planes: planeReducer
[01:30:16]   6 | );
[01:30:16]   7 |

关于如何正确修改代码以修复 flow 错误并同时保持应用程序无错误运行的任何想法?

谢谢!

【问题讨论】:

【参考方案1】:

请尝试在文件的第一行添加@flow pragma 注释。可能和babel问题有关:https://github.com/babel/babel/issues/9240.

编辑: 有undocumented all option in flow-strip-typesbabel-preset-expo 在内部使用它)。

你需要在你的 babel 配置中覆盖它:

overrides: [
    plugins: [
        ['@babel/plugin-transform-flow-strip-types', all: true],
    ]
]

【讨论】:

以上是关于A 缺少类型注释。A 是在函数类型中声明的类型参数的主要内容,如果未能解决你的问题,请参考以下文章

c语言中的类型名和形式参数名的意义是啥

当一个函数无返回值时,函数的类型应定义为啥

TS 函数重载

声明带参数的函数

pydantic学习与使用-3.Typing 类型中的 Optional 和 Union

Python参数类型