Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript - Get typeof function arguments

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).

like image 679
BezR Avatar asked Dec 21 '17 20:12

BezR


People also ask

How can you get the type of arguments passed to a function?

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.

How do you pass a function as argument in TypeScript?

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.

How do you find the return type of a 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.

Does typeof work in TypeScript?

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.


2 Answers

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' ];
like image 147
Morglod Avatar answered Sep 23 '22 08:09

Morglod


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;
like image 29
ShengjieLu Avatar answered Sep 23 '22 08:09

ShengjieLu