flowjs:调用方法`join`。方法不能调用混合 [英] flowjs: call of method `join`. Method cannot be called on mixed

查看:156
本文介绍了flowjs:调用方法`join`。方法不能调用混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个恒定的动物,我用

 导入动物,从'./animals 

说动物常数是:

  {
hoofed:[
'horses',
'sheep',
'goats'
],
feline:[
'lions',
'tigers'
],
canine:[
'dogs',
'wolves'
]
假设我想为一些动物api构造一个Url查询,并写下面的代码:



<

  const fq = Object.values(animals).reduce((memo,animalList)=>备注+`动物:$ {动物列表('动物:')}`,'')

用动物和空白字分隔,然后URL编码并正确匹配目标API表面。



然而,FlowJS拒绝使用以下代码:

  1 3:const fq = Object.values(animals).reduce((memo,animalList)=>备注+`动物:$ {animalList.join('animal:')}`,'')
^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^调用方法`join`。方法不能在
13上调用:const fq = Object.values(animals).reduce((memo,animalList)=> memo +`animal:$ {animalList.join('animal:')}` '')
^^^^^^^混合

我的问题是:为什么?数组很明显不是混合的,它是一个常量,Object.values 应该返回某些类型[string]?



什么是流检测,如何避免?

解决方案

Object.values 函数此时返回一个 Array< mixed> 类型。所以你需要把它转换成一个字符串数组,如下所示。

  let fq:string [] [] =(Object.values(animals):any); 

fq = fq
.reduce((memo,animalList)=>备注+动物:$ {animalList.join('animal:')}`,'')

或使用这样的内联转换:

  const fq =((Object.values(animals):any):string [] [])
.reduce((memo,animalList)=>备注+ $ {animalList.join('animal:')}`,'')

因为 Object.values 函数是在第4阶段作为提案。所以这不是语言的一部分。应该只是一个时间的问题,您不需要解决这个问题的一个演员,只要 Object.values 函数是EcmaScript标准的一部分,流程团队可能会正确实施。


Say I have a constant animals, which I import with

import animals from './animals

Say the animals constant is:

{
  hoofed:[
    'horses',
    'sheep',
    'goats'
],
  feline: [
  'lions',
  'tigers'
],
  canine: [
    'dogs',
    'wolves'
  ]
}

Say that I want to construct a Url query to some animals api, and write the following code:

const fq = Object.values(animals).reduce((memo, animalList) => memo + `animal:${animalList.join(' animal:')} `, '')

Now this code generates a string of animals, separated by the word animal and whitespace, which is then URL encoded and correctly matches the target API surface.

FlowJS however, rejects this code with:

13:   const fq = Object.values(animals).reduce((memo, animalList) => memo + `animal:${animalList.join(' animal:')} `, '')
                                                                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call of method `join`. Method cannot be called on
 13:   const fq = Object.values(animals).reduce((memo, animalList) => memo + `animal:${animalList.join(' animal:')} `, '')
                                                                                                ^^^^^^^^ mixed

My question is: why? The array is quite clearly not mixed, it is a constant, and Object.values should be returning something of type [string]?

What is flow detecting, and how can I avoid it?

解决方案

The Object.values function returns an Array<mixed> type at the moment. So you need to cast it to an array of string arrays like the following.

let fq: string[][] = (Object.values(animals): any);

fq = fq
    .reduce((memo, animalList) => memo + `animal:${animalList.join(' animal:')} `, '')

Or use an inline cast like this:

const fq = ((Object.values(animals): any): string[][])
    .reduce((memo, animalList) => memo + `animal:${animalList.join(' animal:')} `, '')

Flow can't support this function at the moment out of the box, because the Object.values function is in stage-4 as a proposal. So it's not part of the language yet. It should be just a matter of time that you won't need to workaround this problem with a cast, as soon as the Object.values function is part of the EcmaScript standard, the flow team will probably implement it correctly.

这篇关于flowjs:调用方法`join`。方法不能调用混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆