简体   繁体   English

如何避免ES6箭头功能上的Flow类型错误

[英]How to avoid Flow-type error on ES6 arrow function

I am having trouble resolving a Flow-type warning 我无法解决Flow类型警告

When this code block is checked 检查此代码块时

export const login = (email: string, password: string) => ({
  type: types.LOGIN,
  responseTypes: [types.LOGIN_SUCCESS, types.LOGIN_FAILURE],
  promise: (client: Client) => client.post('/auth/sign_in', { email, password }),
});

I receive the warning 我收到了警告

error Unexpected parentheses around single function argument having a body with no curly braces arrow-parens error单个函数参数周围的意外括号,其主体没有花括号arrow-parens

However, when I remove the parenthesis around (client: Client) , I receive the error 但是,当我删除括号(client: Client) ,我收到错误

Module build failed: SyntaxError: Unexpected token 模块构建失败:SyntaxError:意外的令牌

at the colon after client . client后的冒号。

Changing the function to the following 将功能更改为以下内容

export const login = (email: string, password: string) => ({
  type: types.LOGIN,
  responseTypes: [types.LOGIN_SUCCESS, types.LOGIN_FAILURE],
  promise: (client: Client) => { return client.post('/auth/sign_in', { email, password }); },
});

the returns the following warning: 返回以下警告:

error Unexpected block statement surrounding arrow body arrow-body-style error包含箭头体箭头体样式的意外块语句

I'm a little confused at to what the correct syntax would be to fix this warning. 我对修复此警告的正确语法有点困惑。 Thanks. 谢谢。

The error you get is from eslint arrow-parens rule, not from flow. 您得到的错误来自eslint arrow-parens规则,而不是来自flow。

You can resolve it by changing your eslint config or trying the ES6 method syntax : 您可以通过更改eslint配置或尝试ES6方法语法来解决此问题:

promise(client: Client) {
  return client.post('/auth/sign_in', { email, password })
},

I think the issue is in your first function, you are wrapping the body in parenthesis when it should be braces. 我认为这个问题出现在你的第一个函数中,你将括号中的正文包裹在括号中。

export const login = (email: string, password: string) => { 
  type: types.LOGIN, 
  responseTypes: [
    types.LOGIN_SUCCESS, types.LOGIN_FAILURE
  ], 
  promise: (client: Client) => client.post('/auth/sign_in', { email, password }), 
};

You can also write it this way (without using the single expression shorthand. 你也可以用这种方式编写它(不使用单个表达式的简写。

export const login = (email: string, password: string) => { 
  type: types.LOGIN, 
  responseTypes: [
    types.LOGIN_SUCCESS, types.LOGIN_FAILURE
  ], 
  promise: (client: Client) => { return client.post('/auth/sign_in', { email, password }); }, 
};

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

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