简体   繁体   English

&#39;Set 类型的错误参数<string> &#39; 不能分配给类型为 &#39;string&#39; 的参数

[英]error Argument of type 'Set<string>' is not assignable to parameter of type 'string'

I have two property :我有两个财产:

  roleIdFormIdMap: Map<string, Set<string>>;
  appIdFormIdMap: Map<string, Set<string>>;

I have set the value on both property.我已经在这两个属性上设置了值。

Now i need to retrieve the value :现在我需要检索值:

let formIds = this.props.roleIdFormIdMap.get(securityRoleId);

const everyoneFormIds = this.props.roleIdFormIdMap.get(everyoneSecurityRole);
if (formIds && everyoneFormIds) {
 ///how to add value , here I am getting the error Argument of type 'Set<string>' is not assignable 
  to parameter of type 'string'.ts(2345)
  formIds.add(everyoneFormIds);
}

if (appId && appId !== this.appNoSelectionKey && formIds) {
  const formIdsBasedOnApp = this.props.appIdFormIdMap.get(appId);
  //same thing here , how to acheive it
  formIds = _.intersectionWith(formIdsBasedOnApp, formIds, _.isEqual);
}

I would do something like this to aggregate sets:我会做这样的事情来聚合集合:

 const a = new Set([1,2,3]) const b = new Set([3,4,5]) // Aggregate results from a to b a.forEach((v) => b.add(v)) console.log([...b])

To my understanding the method Set.add only admits one argument and denies iterators Set javascript据我了解 Set.add 方法只接受一个参数并拒绝迭代器Set javascript

  • formIds type here is Set<string> | undefined这里的formIds类型是Set<string> | undefined Set<string> | undefined , so you will not be directly able to call .add on it Set<string> | undefined ,所以你不能直接调用.add就可以了
  • you can call it this way: formIds?.add(???)你可以这样称呼它: formIds?.add(???)
  • now add function on Set takes string , so you wont be able to pass everyoneFormIds here because type of everyoneFormIds is Set<string> | undefined现在在Setadd函数需要string ,所以你不能在这里传递everyoneFormIds因为everyoneFormIds类型是Set<string> | undefined Set<string> | undefined

Try this:尝试这个:

everyoneFormIds.forEach((id) => formIds.add(id))

暂无
暂无

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

相关问题 'string | 类型的参数 undefined' 不可分配给“string”类型的参数 - TypeScript 错误 - Argument of type 'string | undefined' is not assignable to parameter of type 'string' - TypeScript error Typescript 枚举键入错误(“字符串”类型的参数不可分配给类型参数) - Typescript enums typing error (argument of type 'string' is not assignable to parameter of type) 令人困惑的编译器错误:“字符串”类型的参数不可分配给类型参数 - Confusing compiler error: Argument of type 'string' is not assignable to parameter of type “数字”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'number' is not assignable to parameter of type 'string' 日期类型的参数不能分配给字符串类型的参数 - Argument of type date is not assignable to parameter of type string “文件”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'File' is not assignable to parameter of type 'string' '[string]' 类型的参数不可分配给 'U' 类型的参数 - Argument of type '[string]' is not assignable to parameter of type 'U' “字符串”类型的参数不能分配给“数字”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'number' “T”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'T' is not assignable to parameter of type 'string' “RegExp”类型的参数不能分配给“string”类型的参数 - Argument of type 'RegExp' is not assignable to parameter of type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM