我具有函数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[]
}
答案 0 :(得分:3)
没有问题,因为实现Flight
接口的每个对象还通过通过HasSectors
的继承来实现BasicFlightInfo
。
这意味着只要期望使用HasSectors
,就可以使用任何实现它的对象(即,任何实现HasSectors
,BasicFlightInfo
或Flight
的对象)。
一切正常,没有“类型不匹配”。