简体   繁体   English

“string[]”类型的参数不可分配给“string”类型的参数

[英]Argument of type 'string[]' is not assignable to parameter of type 'string'

Have an array called categoryImages that I'm trying to pass into a function, and get the following error:有一个名为 categoryImages 的数组,我试图将它传递给一个函数,并得到以下错误:

"Argument of type 'string[]' is not assignable to parameter of type 'string'". “'string[]' 类型的参数不能分配给 'string' 类型的参数”。

const getLink = (
    groupCode: string,
    groupDisplayName: string,
    groupCount?: number,
    groupImage?: string
  )

const categoryImages = [
        attachmentImage,
        compactorImage,
        telehandlerImage,
        backhoeLoaderImage,
        excavatorImage,
        wheelLoaderImage,
        compactTrackLoaderImage,
        skidSteerLoaderImage,
      ];

} else if (Constants.isEnvironmentBFE) {
          count++;
          const newLink = getLink(
            // Grabbing API values
            group["group-code"],
            group["group-display-name"],
            group.count,
            // Load in local images
            categoryImages // <-- throws error here
          );
          categoryList[column].push(newLink);
          endColumnCheck(count);
        }

I basically want to output this array of images, into the DOM.我基本上想将这个图像数组输出到 DOM 中。

You've said that the “getLink” function takes an argument “groupImage” which is a single image, But when you call the function you are giving it an array of images “categoryImages”.你已经说过“getLink”函数接受一个参数“groupImage”,它是一个单一的图像,但是当你调用这个函数时,你给它一个图像数组“categoryImages”。

You can:你可以:

  • call “getLink” with categoryImages[0] instead of the whole array.使用 categoryImages[0] 而不是整个数组调用“getLink”。 Since the image is optional this will be ok even if the array is empty.由于图像是可选的,即使数组为空也可以。
  • loop through the array and call “getLink” separately for each element遍历数组并为每个元素分别调用“getLink”
  • refactor “getLink” to accept an array of images重构“getLink”以接受图像数组

暂无
暂无

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

相关问题 “字符串”类型的参数不可分配给“Blob”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'Blob' 'IAulasAdicionais[]' 类型的参数不可分配给'string' 类型的参数 - Argument of type 'IAulasAdicionais[]' is not assignable to parameter of type 'string' “字符串”类型的参数不可分配给“元素”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'Element' TS2345:“字符串”类型的参数 | undefined&#39; 不能分配给类型为 &#39;string&#39; 的参数。 “未定义”类型不能分配给“字符串”类型 - TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string' 'string | 类型的参数 undefined' 不能分配给'string' 类型的参数。 类型“未定义”不可分配给类型“字符串” - Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string' useState 和 localStorage:“string null”类型的参数不可分配给“string”类型的参数 - useState and localStorage: argument of type 'string null' is not assignable to parameter of type 'string' &#39;string | 类型的参数 null&#39; 不能分配给类型为 &#39;string&#39; 的参数 - Argument of type 'string | null' is not assignable to parameter of type 'string' 'string' 类型的参数不能分配给 '{ name: string; 类型的参数; }' - Argument of type 'string' is not assignable to parameter of type '{ name: string; }' React,typescript - 'string | 类型的参数 null' 不能分配给“字符串”类型的参数 - React, typescript - Argument of type 'string | null' is not assignable to parameter of type 'string' &#39;Set 类型的错误参数<string> &#39; 不能分配给类型为 &#39;string&#39; 的参数 - error Argument of type 'Set<string>' is not assignable to parameter of type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM