typescript - 打字稿修改嵌套类型
问题描述
我是打字稿的新手,不知道如何修改深层嵌套接口。
我有接口(从 graphql 模式生成),我想将其转换为新模型
看起来像这样
interface SeatMap{
// other fields
segments: {
// other fields
rows: { // <-- i want to add one more field to row type
// other fields
seats: { // <-- i want to add few new fields to seat type
isAvailable: boolean;
isAisle: boolean;
// etc.
}
}
}
}
比,我创建了两个新功能
addServiceFieldToSeats 接收 SeatMap 并返回带有修改后的座位类型的 SeatMap
和 addRowPartsToRows 接收 SeatMap 并返回带有修改的行类型的 SeatMap
然后通过 redux 中的 compose 函数创建新函数seatMapAdapter
,但不知道如何解决打字稿错误
我知道我的函数将座位图行类型转换为新类型 RowWithParts 但addServiceFieldToSeats
接收旧座位图类型
我如何描述每个 SeatMap 转换?
解决方案
经过几天的尝试,看起来我找到了解决方案:
type NestedModelTransition<OriginalType, Compare, ReplaceWith> = {
[Key in keyof OriginalType]: OriginalType[Key] extends Compare
? ReplaceWith
: NestedModelTransition<OriginalType[Key], Compare, ReplaceWith>
};
type SeatMapWithSeatService = NestedModelTransition<SeatMap_SeatMap, SeatMap_SeatMap_segments_decks_rows_seats, Seat>;
type SeatMapWithRowWithParts = NestedModelTransition<
SeatMapWithSeatService,
SeatMap_SeatMap_segments_decks_rows,
RowWithParts
>;
推荐阅读
- javascript - 替换后清理逗号和空格
- javascript - 滚动时的Angular 5粘性菜单
- go - 将整个 golang 包推送到 github
- credentials - 无法访问 Dialogflow 应用程序的正确应用程序默认凭据
- javascript - 在 mat-grid 中使用 mat-button-toggle-group
- angular - 错误:InvalidPipeArgument:管道“DatePipe”的“2018-05-28 15:17:47”
- html - 博主:如何完全禁用 Lightbox?
- c++ - 是否应该使用别名或来源名称作为前向声明?
- mongodb - 快速路由器补丁 findoneandupdate 不起作用
- javascript - Tensorflowjs - optimize.minimize 找不到任何变量与损失函数的结果之间的联系