简体   繁体   English

类型'string |的参数 编号 string []'不可分配给'string'类型的参数

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

I have code in my typescript file 我的打字稿文件中有代码

Here is code 这是代码

slide(event, ui) {
    $(`#stops_${i}`).val(`Max ${ui.value} ${__('byten')}`);
    $(`#filter_stops_${i}`).val(ui.value);
    if ((ui.value > 0) && ($('#filter_direct').val() === '1')) {
      $('#filter_direct').parent().children('.Switch').removeClass('On').addClass('Off');
      $('#filter_direct').val('0');
    }
    let stop_sum = 0;
    $('.filter_stops').each(function() {
      return stop_sum += parseInt($(`#filter_stops_${$(this).attr('id').split('_')[1]}`).val());
    });
    if ((stop_sum === 0) && ($('#filter_direct').val() === '0')) {
      $('#filter_direct').parent().children('.Switch').removeClass('Off').addClass('On');
      return $('#filter_direct').val('1');
    }
  }

But at this row: 但是在这一行:

return stop_sum += parseInt($(`#filter_stops_${$(this).attr('id').split('_')[1]}`).val());

when I try to do split I have this error. 当我尝试拆分时,出现此错误。

[ts] Argument of type 'string | [ts]类型'string |的参数 number | 编号 string[]' is not assignable to parameter of type 'string'. string []'不可分配给'string'类型的参数。 Type 'number' is not assignable to type 'string'. 不能将“数字”类型分配给“字符串”类型。

How I can fix it? 我该如何解决?

val can return one of several types depending on the underling type of the html element. val可以返回几种类型之一,具体取决于html元素的基础类型。 The simplest solution if you are sure this will always be a string is to use a type assertion to string 如果您确定这将始终是string那么最简单的解决方案是使用类型断言来string

return stop_sum += parseInt($(`#filter_stops_${$(this).attr('id').split('_')[1]}`).val() as string);

Another option is to use type guards to distinguish between cases: 另一种选择是使用类型保护来区分大小写:

let value = $(`#filter_stops_${$(this).attr('id').split('_')[1]}`).val();
if (typeof value === "string") {
    return stop_sum += parseInt(value);
}
if (typeof value === "number") {
    return stop_sum += value;
} else {
    throw Error("Unsupported value type")
}

暂无
暂无

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

相关问题 “数字”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'number' is not assignable to parameter of type 'string' “字符串”类型的参数不能分配给“数字”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'number' 'string | 类型的参数 number' 不能分配给类型为 'string' 的参数。 “数字”类型不能分配给“字符串”类型 - Argument of type 'string | number' is not assignable to parameter of type 'string'. Type 'number' is not assignable to type 'string' 参数类型编号不可分配给参数类型字符串 | 未定义类型编号不可分配给类型字符串 - Argument type number is not assignable to parameter type string | undefined Type number is not assignable to type string 参数类型Number | Function不能赋予参数类型Number - Argument type Number is not assignable to parameter type String|Function 'number' 类型的参数不能使用 ParseInt 分配给'string' 类型的参数 - Argument of type 'number' is not assignable to parameter of type 'string' using ParseInt 如何解决“'number' 类型的参数不能分配给'string | RegExp' 类型的参数。” - How to fix "Argument of type 'number' is not assignable to parameter of type 'string | RegExp'." 如何修复“数字”类型的参数不可分配给“字符串”类型的参数 | 正则表达式'' - How to fix ' Argument of type 'number' is not assignable to parameter of type 'string | RegExp' ' 日期类型的参数不能分配给字符串类型的参数 - Argument of type date is not assignable to parameter of type string “文件”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'File' is not assignable to parameter of type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM