承认我有一个像这样的函数
const createPerson = () => ({ firstName: 'John', lastName: 'Doe' })
我如何在声明createPerson
之前,无需声明接口或类型,获取返回值类型?
类似于这样:
type Person = typeof createPerson()
示例场景
我有一个 Redux 容器,将状态和派发操作映射到组件的 props 上。
containers/Counter.tsx
import { CounterState } from 'reducers/counter'
// ... Here I also defined MappedState and mapStateToProps
// The interface I would like to remove
interface MappedDispatch {
increment: () => any
}
// And get the return value type of this function
const mapDispatchToProps =
(dispatch: Dispatch<State>): MappedDispatch => ({
increment: () => dispatch(increment)
})
// To export it here instead of MappedDispatch
export type MappedProps = MappedState & MappedDispatch
export default connect(mapStateToProps, mapDispatchToProps)(Counter)
组件/Counter.tsx
import { MappedProps } from 'containers/Counter'
export default (props: MappedProps) => (
<div>
<h1>Counter</h1>
<p>{props.value}</p>
<button onClick={props.increment}>+</button>
</div>
)
我希望能够导出mapDispatchToProps
的类型,而不必创建MappedDispatch
接口。
我在此处缩减了代码,但这使我需要两次输入相同的内容。