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()
I have a Redux container that maps state and dispatch actions to props of a component.
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)
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.
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.
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.
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.
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.
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.
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;
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.
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:
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With