【问题标题】:warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any OR Property 'map' does not exist on type 'string'.ts(2339)警告 任何意外。指定不同的类型 @typescript-eslint/no-explicit-any 或属性 'map' 不存在于类型 'string'.ts(2339)
【发布时间】:2020-03-11 05:49:33
【问题描述】:

我有以下 = 代码。我想知道这是否是 any 的适当使用。我收到了 Unexpected any. Specify a different type @typescript-eslint/no-explicit-any. 的 lint 警告

但是,如果我将其更改为字符串。我收到地图错误,Property map does not exist on type string. 该功能完全按照我的意愿工作。是否适用于 ANY 或有办法解决它?

代码

function flatMapArr(object: Array<any>): Array<string> {
  return Object.entries(object).reduce(
    (acc: Array<string>, [k, v]) => acc.concat([k, ...v.map((s: Array<string>) => `${s} ${k}`)]),
    [],
  )
}

采用数组形式

{
      "arctic":[],
      "atlantic":[
             "north",
             "south",
      ],
      "indian":[],
      "pacific":[
             "north",
             "south",
      ],
      "southern":[],
} 

【问题讨论】:

    标签: typescript eslint array.prototype.map


    【解决方案1】:
    export interface Oceans {
        artic:    string;
        atlantic: string[];
        indian:   string;
        pacific:  string[];
        southern: string;
    }
    

    您可以将您的对象模式定义为像上面这样的接口,并将 Oceans 用作“类型”。我需要更多信息,但乍一看,Interface 似乎是解决方案

    【讨论】:

      猜你喜欢
      • 2020-02-16
      • 2021-11-09
      • 2021-12-28
      • 1970-01-01
      • 2020-10-08
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      相关资源
      最近更新 更多