如何管理Typescript中未定义的返回类型?

如何管理Typescript中未定义的返回类型?,typescript,Typescript,抱歉,我是Typescript新手,需要在过滤后将道具传递给组件,因为我定义了一个简单的过滤方法。我得到一个编译错误,说“Type”IMilestone[]| undefined“不可分配给Type”IMilestone[]” 因为我获取并填充studyProgress,所以它可以是未定义的。定义此方法的正确方法是什么?const milestonesofcativegroup=:IMilestone[]| undefined=>{ return studyProgress?.groups.fi

抱歉,我是Typescript新手,需要在过滤后将道具传递给组件,因为我定义了一个简单的过滤方法。我得到一个编译错误,说“Type”IMilestone[]| undefined“不可分配给Type”IMilestone[]”

因为我获取并填充studyProgress,所以它可以是未定义的。定义此方法的正确方法是什么?

const milestonesofcativegroup=:IMilestone[]| undefined=>{ return studyProgress?.groups.filtergroup:IGroup=>group.name==activeGroup[0]。里程碑; } 您声明如果studyProgress未定义或为null,则返回未定义的studyProgress?,因此您应该使用未定义的IMilestone[]|null.声明返回类型是Union,其含义是它将返回IMilestone[]或未定义。

const milestonesOfActiveGroup=:IMilestone[]未定义=>{ return studyProgress?.groups.filtergroup:IGroup=>group.name==activeGroup[0]。里程碑; } 您声明如果studyProgress未定义或为null,则返回未定义的studyProgress?,因此您应该使用未定义的IMilestone[]|null.声明返回类型是Union,其含义是它将返回IMilestone[]或undefined。

您可以将其定义为IMilestone[]| undefined

您可以将其定义为IMilestone[]|未定义


您在studyProgress上使用了可选链接,这意味着该变量可能为null或未定义。在长格式中,您的函数执行以下操作:

const milestonesOfActiveGroup = (): IMilestone[] => {
  if (studyGroup) {
    return studyProgress.groups.filter(
      (group: IGroup) => group.name === activeGroup)[0].milestones
    )
  } else { 
    return undefined // this does not match the declared return type
  }
}
所以,如果studyGroup不存在,您就不能钻取它,而是返回undefined

要处理这种情况,你有几个选择。这里有两个

允许函数返回未定义的值

或者,如果studyProgress确实应该在那里,那么抛出一个错误,如果它不在那里,那么它应该被认为是一个bug

const milestonesOfActiveGroup = (): IMilestone[] => {
  if (!studyProgress) throw new Error("studyGroup has no value!")
  // typescript knows that studyProgress must exist from here on.
  return studyProgress.groups.filter((group:IGroup) => group.name === activeGroup)[0].milestones)
}

想想当studyProgress没有设置值时意味着什么,以及在这种情况下您的程序应该做什么。然后处理这种情况,typescript应该会给您开绿灯。

您在studyProgress上使用了可选链接,这意味着变量可能为null或未定义。在长格式中,您的函数执行以下操作:

const milestonesOfActiveGroup = (): IMilestone[] => {
  if (studyGroup) {
    return studyProgress.groups.filter(
      (group: IGroup) => group.name === activeGroup)[0].milestones
    )
  } else { 
    return undefined // this does not match the declared return type
  }
}
所以,如果studyGroup不存在,您就不能钻取它,而是返回undefined

要处理这种情况,你有几个选择。这里有两个

允许函数返回未定义的值

或者,如果studyProgress确实应该在那里,那么抛出一个错误,如果它不在那里,那么它应该被认为是一个bug

const milestonesOfActiveGroup = (): IMilestone[] => {
  if (!studyProgress) throw new Error("studyGroup has no value!")
  // typescript knows that studyProgress must exist from here on.
  return studyProgress.groups.filter((group:IGroup) => group.name === activeGroup)[0].milestones)
}
想想当studyProgress没有设置值时意味着什么,以及在这种情况下您的程序应该做什么。然后处理那个案子,打字本会给你开绿灯的

const milestonesOfActiveGroup = (): IMilestone[] => {
  if (!studyProgress) throw new Error("studyGroup has no value!")
  // typescript knows that studyProgress must exist from here on.
  return studyProgress.groups.filter((group:IGroup) => group.name === activeGroup)[0].milestones)
}