相关疑难解决方法(0)

TypeScript typeof函数返回值

承认我有这样的功能

const createPerson = () => ({ firstName: 'John', lastName: 'Doe' })
Run Code Online (Sandbox Code Playgroud)

如果在声明之前没有声明接口或类型,我怎么能createPerson得到返回值类型?

像这样的东西:

type Person = typeof createPerson()
Run Code Online (Sandbox Code Playgroud)

示例场景

我有一个Redux容器,它将状态和调度操作映射到组件的props.

集装箱/ 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: () => …
Run Code Online (Sandbox Code Playgroud)

typescript

27
推荐指数
3
解决办法
1万
查看次数

标签 统计

typescript ×1