typescript - 打字稿将类型分配给另一个类型中的类型的对象
问题描述
我有这样的类型:
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
。但是我如何分配有效载荷的类型(这是{id:string, otherId:string, contentId:string}
通过从类型中自动选择该类型来自动分配的,这样ReturnType<typeof reorderPiece>
就x
可以拥有该类型并且我可以将其访问为or ?typeof ReturnType<typeof reorderPiece>.payload
x
{id:string, otherId:string, contentId:string}
x.id
x.contentId
我知道我总是可以显式(或隐式)编写此类类型并在我的方法中使用该类型,但我想知道是否可以从方法中自动选择该类型。
(我在 Typescript 3.7.5 上)
解决方案
推荐阅读
- xamarin.forms - Xamarin.Forms 响应式设计
- scala - 如何替换所有数字和“。” 通过 Dataframes spark scala 中列的“”
- vuejs2 - 在 Nuxt 错误页面中访问原始错误对象
- python - 西里尔编码 python 2.7 数组
- javascript - Bootstrap Carousel 忽略数据间隔
- reactjs - 在 React 中安装组件后如何创建 Refs?
- android - 在同一活动中显示警报对话框
- pandas - 从熊猫数据框中计算峰值变化
- javascript - 获取交叉过滤器中的所有过滤值
- python - 如何将未来的观察纳入 Keras 自定义损失函数?