【发布时间】:2020-06-29 16:18:31
【问题描述】:
我有一个这样的类型:
export function reorderPiece(contentId:string, id:string, otherId:string){
return {
type: REORDER_PIECE_PENDING,
payload: {id, otherId, contentId}
}
}
此操作通过管道(此时我无法重构),在另一个函数中,我需要将有效负载类型分配给一个对象。
我可以很容易地得到reorderPiece的返回类型:
x:ReturnType<typeof reorderPiece> 并访问 x.payload。但是我如何分配有效负载的类型(通过从ReturnType<typeof reorderPiece> 类型中选择该类型自动分配{id:string, otherId:string, contentId:string} 以便x 类似于typeof ReturnType<typeof reorderPiece>.payload 以便x 将具有{id:string, otherId:string, contentId:string} 类型和我可以x.id 或x.contentId 访问它吗?
我知道我总是可以显式(或隐式)编写此类类型并在我的方法中使用该类型,但我想知道是否可以从方法中自动选择该类型。
(我使用的是 Typescript 3.7.5)
【问题讨论】: