简体   繁体   中英

Missing type annotation for A. A is a type parameter declared in function type

I have the following code:

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

export default combineReducers({
    planes: planeReducer
});

which runs properly when running:

> expo start

Then, when I run flow with the following command:

>  npm run flow

I get the following flow error:

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>;

Then, when I modify the code above by adding: <any, any> as follows:

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

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

when I run flow again as before, the flow error goes away, but if I run again:

> expo start

I get the following runtime error:

[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 |

Any idea on how to modify the code properly in order fix that flow error and at the same time keep the applicatioin running with no errors?

Thanks!

Please try adding @flow pragma comment in the first line of the file. It's probably related to the babel issue: https://github.com/babel/babel/issues/9240 .

Edit : There's undocumented all option in flow-strip-types ( babel-preset-expo makes use of it internally).

You need to overwrite it in your babel config:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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