TypeScript typeof 函数返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40590034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:00:42  来源:igfitidea点击:

TypeScript typeof Function return value

typescript

提问by kube

Admit I have a function like this

承认我有这样的功能

const createPerson = () => ({ firstName: 'John', lastName: 'Doe' })

How can I, without declaring an interface or a typebefore declaring createPerson, get the return value type?

我怎样才能,不宣接口或类声明之前createPerson,得到返回值的类型?

Something like this:

像这样的东西:

type Person = typeof createPerson()


Example Scenario

示例场景

I have a Redux container that maps state and dispatch actions to props of a component.

我有一个 Redux 容器,它将状态和分派动作映射到组件的道具。

containers/Counter.tsx

容器/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)

components/Counter.tsx

组件/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>
)

I want to be able to export the type of mapDispatchToPropswithout having to create MappedDispatchinterface.

我希望能够导出类型而mapDispatchToProps无需创建MappedDispatch接口。

I reduced the code here, but it makes me type the same thing two times.

我在这里减少了代码,但它让我输入了两次相同的东西。

回答by kube

Original Post

原帖

TypeScript < 2.8

打字稿 < 2.8

I created a little library that permits a workaround, until a fully declarative way is added to TypeScript:

我创建了一个允许解决方法的小库,直到将完全声明性的方式添加到 TypeScript:

https://npmjs.com/package/returnof

https://npmjs.com/package/returnof

Also created an issue on Github, asking for Generic Types Inference, that would permit a fully declarative way to do this:

还在 Github 上创建了一个问题,要求Generic Types Inference,这将允许以完全声明的方式执行此操作:

https://github.com/Microsoft/TypeScript/issues/14400

https://github.com/Microsoft/TypeScript/issues/14400



Update February 2018

2018 年 2 月更新

TypeScript 2.8

打字稿 2.8

TypeScript 2.8 introduced a new static type ReturnTypewhich permits to achieve that:

TypeScript 2.8 引入了一种新的静态类型ReturnType,可以实现:

https://github.com/Microsoft/TypeScript/pull/21496

https://github.com/Microsoft/TypeScript/pull/21496

You can now easily get the return type of a function in a fully declarative way:

您现在可以以完全声明的方式轻松获取函数的返回类型:

const createPerson = () => ({
  firstName: 'John',
  lastName: 'Doe'
})

type Person = ReturnType<typeof createPerson>

回答by 0x6adb015

This https://github.com/Microsoft/TypeScript/issues/4233#issuecomment-139978012might help:

这个https://github.com/Microsoft/TypeScript/issues/4233#issuecomment-139978012可能有帮助:

let r = true ? undefined : someFunction();
type ReturnType = typeof r;

回答by Mihai R?ducanu

Adapted from https://github.com/Microsoft/TypeScript/issues/14400#issuecomment-291261491

改编自https://github.com/Microsoft/TypeScript/issues/14400#issuecomment-291261491

const fakeReturn = <T>(fn: () => T) => ({} as T)

const hello = () => 'World'
const helloReturn = fakeReturn(hello) // {}

type Hello = typeof helloReturn // string

The example in the link uses null as Tinstead of {} as T, but that breaks with Type 'null' cannot be converted to type 'T'.

链接中的示例使用null as T而不是{} as T,但这与Type 'null' cannot be converted to type 'T'.

The best part is that the function given as parameter to fakeReturnis not actually called.

最好的部分是作为参数给出的函数fakeReturn实际上并没有被调用。

Tested with TypeScript 2.5.3

使用 TypeScript 2.5.3 测试



TypeScript 2.8 introduced some predefined conditional types, including the ReturnType<T>that obtains the return type of a function type.

TypeScript 2.8 引入了一些预定义的条件类型,包括ReturnType<T>获取函数类型的返回类型的 。

const hello = () => 'World'

type Hello = ReturnType<typeof hello> // string