Is it possible to get the type of a function's arguments with TypeScript?
I have a function that returns another function that returns a promise:
type login = (params: LoginParams) => dispatched
type dispatched = (dispatch: Dispatch) => Promise<any>
I would like to create a generic type that takes function as a param and creates a new function type. For example:
GenericFunctionType<login>
Would create the following type:
(LoginParams) => Promise<any>
In short GenericFunctionType creates a function type with the arguments of the initial function (login) and return value of the returned function (dispatched).
I want this because I am using Redux. Here is my use case:
//Action Creator
export type login = (params: LoginParams) => ThunkAction<Promise<any>>;
export const login: login = params => async dispatch => {
dispatch({ type: LOGIN });
const promise = httpClient.post('/auth/sign_in', params);
...stuff
return promise;
};
My Login component is connected like so:
interface LoginProps {
login: ???
}
class Login extends React.component<LoginProps, {}> {...}
connect(null, {login})(Login)
I'm trying to find a reusable way of typing action creators that have been wrapped in dispatch (which is what connect
does).
There are two ways to pass arguments to a function: by reference or by value. Modifying an argument that's passed by reference is reflected globally, but modifying an argument that's passed by value is reflected only inside the function.
Similar to JavaScript, to pass a function as a parameter in TypeScript, define a function expecting a parameter that will receive the callback function, then trigger the callback function inside the parent function.
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.
TypeScript comes with some built-in type guards: typeof and instanceof . They're very useful, but have limited scope. For example, typeof can only be used to check string , number , bigint , function , boolean , symbol , object , and undefined types.
Use Parameters type from default library
Old answer
You can use tsargs package from npm for this.
Your case:
import { Arg1 } from 'tsargs';
type GenericFunctionType<F extends Function> = (params: Arg1<F>) => Promise<any>;
Examples with tsargs:
import { Arg2, Args2off1, ArgsN } from 'tsargs';
function foo(a: boolean, b: number, c: string) {}
// pick specific argument
const secondArg: Arg2<typeof foo> = 123;
// pick 2 arguments skipping first
const argsBC: Args2off1<typeof foo> = [ 123, 'Hello' ];
// pick all arguments
const argsABC: ArgsN<typeof foo> = [ true, 123, 'Hello' ];
For TypeScript 3.0
We can get function arguments type as tuple types like below
type ArgsType<T> = T extends (...args: infer U) => any ? U : never;
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