打字稿未检测到类型错误

时间:2018-09-12 10:46:57

标签: typescript

我具有函数findFlightRecommendation的功能。它返回Flight类型。

export const findFlightRecommendation = <T>(state: IStore, sectorIds: T): Flight => {
  if (!state || !state.search || !state.search.results) {
    return undefined
  }
  ...
  return state.search.results.find(r => _.isEqual(r.sectorsId, sectorIds))
}

此功能用于查找航班:

// state.personalData.sectorsKey is string[]
const flight = findFlightRecommendation(state, state.personalData.sectorsKey) 

flight对象后来在其他函数上用作参数。

formatLoadingSubtitle(props.t, flight, getTravellers(state.form.searchForm.values)

但是formatLoadingSubtitle类型声明说,排期应为HasSectors

export const formatLoadingSubtitle = (t: Translate, flight: HasSectors, travellers: number) => {
  const departure = getDepartureTime(flight)
  const arrival = getReturnTime(flight)

  if (departure) {
    return formatFlightDetails(t, departure, arrival, travellers)
  }
}

类型:

export interface Flight extends BasicFlightInfo {
  mtk?: boolean
  sectorsId?: string[]
  sectorCodes?: string[]
  handBaggage?: IHandBaggage
  checkedBaggageIncluded?: boolean
  leftSeats?: number
  topRecommendationTag?: FilterSortByTypes
  recommendationPriority?: number
}

export interface BasicFlightInfo extends HasSectors, FlightDebugInfo {
  totalPrice: number
  paxPrices: IPaxPrices
  currency: string
  offerFees: IOfferFees
  totalDiscount?: number
  cabinClasses?: string[]
}

interface HasSectors {
  sectors: ISector[]
}

1 个答案:

答案 0 :(得分:3)

没有问题,因为实现Flight接口的每个对象还通过通过HasSectors的继承来实现BasicFlightInfo

这意味着只要期望使用HasSectors,就可以使用任何实现它的对象(即,任何实现HasSectorsBasicFlightInfoFlight的对象)。

一切正常,没有“类型不匹配”。