Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript typeof Function return value

Tags:

typescript

Admit I have a function like this

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

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

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.

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) 

components/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 mapDispatchToProps without having to create MappedDispatch interface.

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

like image 371
kube Avatar asked Nov 14 '16 13:11

kube


People also ask

What does typeof return in TypeScript?

A typeOf keyword returns the type of an identifier in TypeScript. It also acts as a Type Guard narrowing the Type in the scope where we use it.

How do I return a value from a function in TypeScript?

The function's return type is string. Line function returns a string value to the caller. This is achieved by the return statement. The function greet() returns a string, which is stored in the variable msg.

How do you get a return type of function in TypeScript?

Use the ReturnType utility type to get the return type of a function in TypeScript, e.g. type T = ReturnType<typeof myFunction> . The ReturnType utility type constructs a type that consists of the return type of the provided function type.

What does () => void mean TypeScript?

Introduction to TypeScript void type The void type denotes the absence of having any type at all. It is a little like the opposite of the any type. Typically, you use the void type as the return type of functions that do not return a value.

What is function return type in typescript?

Function return type used when we return value from the function. We can return any type of value from the function or nothing at all from the function in TypeScript. Some of the return types is a string, number, object or any, etc. If we do not return the expected value from the function, then we will have an error and exception.

Why do we use ‘any’ type in typescript?

One advantage of using ‘any’ type in TypeScript is that we can return anything from our function then. By using that, our function is not specific to return number or string; rather, we can return anything from it. Now we will see one sample example for beginners to understand its implementation and usage see below;

Can typescript infer the return type of the longest type?

We allowed TypeScript to infer the return type of longest . Return type inference also works on generic functions. Because we constrained Type to { length: number }, we were allowed to access the .length property of the a and b parameters.

What is the inferred return type of firstelement2 in typescript?

Its inferred return type is Type, but firstElement2 ’s inferred return type is any because TypeScript has to resolve the arr [0] expression using the constraint type, rather than “waiting” to resolve the element during a call. Here’s another pair of similar functions:


1 Answers

Original Post

TypeScript < 2.8

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

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:

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


Update February 2018

TypeScript 2.8

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

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> 
like image 93
kube Avatar answered Sep 21 '22 04:09

kube