我正试图在我的打字稿反应代码中消除重复,我想知道是否有可能从已知的返回类型的函数中创建一个类型?
类似的东西:
const mapStateToProps = (state: StoreState) => ({settings: store.settings})
type Props = ReturnTypeOf<typeof mapStateToProps>
答案 0 :(得分:2)
TypeScript实际上正是以此为条件类型in the handbook的示例:
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : any;
例如:
const mapStateToProps = (state: StoreState) => ({settings: store.settings})
type Props = ReturnType<typeof mapStateToProps>