简体   繁体   English

如何在 typescript 中检索 function 签名列表?

[英]How to retrieve a list of function signatures in typescript?

I have a function我有一个 function

function add (...nums: number[]): number {
  return nums.reduce((a, b) => a + b)
}

how do I grab this signature?我如何抓住这个签名? so that I can use it here这样我就可以在这里使用它

function subtract (
  sum: ReturnType<add>, 
  ...args: [/* <-- Here I want to grab the same type as add's arguments */]
): number {
  return args.reduce((running, next) => running - next  , sum)
}

I would go with Parameters我会用参数go

Parameters<T>参数<T>

Constructs a tuple type of the types of the parameters of a function type T.构造一个 function 类型 T 的参数类型的元组类型。

function subtract (
  sum: ReturnType<typeof add>, 
  ...args: Parameters<typeof add>
): number {
  return args.reduce((running, next) => running - next  , sum)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM