Is there ArgumentsType like ReturnType in Typescript?

前端 未结 6 689
一个人的身影
一个人的身影 2020-11-30 12:34

ReturnType extracts return type of a function.

Is there a way to define ArgumentsType that extracts parameter types of a

6条回答
  •  抹茶落季
    2020-11-30 13:02

    I'm sure there are many uses for Parameters but I wanted a way to provide proxy method to the same method on a private member and it seemed a perfect fit.

    eg. I want to have the following, such that if the authService.login signature changes I don't have to modify my proxy - just whatever is calling it.

    login(username: string, password: string) { this.authService.login(username, password); }
    

    With Parameters you can do the following

    login = (...params: Parameters) => this.authService.login(...params);
    

    Of course the benefit is that if you have complex parameters it'll just take care of itself nicely - (probably wouldn't recommend this for just two strings!)

    It took me a couple attempts to figure out that I needed ... on both occurrences of params.

提交回复
热议问题