簡體   English   中英

&#39;Set 類型的錯誤參數<string> &#39; 不能分配給類型為 &#39;string&#39; 的參數

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

我有兩個財產:

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

我已經在這兩個屬性上設置了值。

現在我需要檢索值:

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);
}

我會做這樣的事情來聚合集合:

 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])

據我了解 Set.add 方法只接受一個參數並拒絕迭代器Set javascript

  • 這里的formIds類型是Set<string> | undefined Set<string> | undefined ,所以你不能直接調用.add就可以了
  • 你可以這樣稱呼它: formIds?.add(???)
  • 現在在Setadd函數需要string ,所以你不能在這里傳遞everyoneFormIds因為everyoneFormIds類型是Set<string> | undefined Set<string> | undefined

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM