【发布时间】:2022-01-16 06:36:02
【问题描述】:
我有一个接受两个参数的函数(下面称为 runLater):
- 任意函数
- 的参数数组 任意函数
像这样:
function runLater(aFunction, aFunctionsParams) {
// store for later use
}
如何键入 runLater 函数,这样当我将函数作为第一个参数传入时,第二个参数将被限制为该函数的参数类型?
function logNameAndAge(name: string, age: number) {...}
runLater(logNameAndAge, ['hoff', 42]) // ok, the parameter types match up
runLater(logNameAndAge, [false, 'oops']) // no ok, someFunction has [string, number] as paramters
【问题讨论】:
-
不传递函数和参数,而是使用闭包传递具有指定参数的函数。
runLater(() => logNameAndAge,('hoff', 42)) -
typescript 有一个名为 Parameters
的实用程序类型
标签: typescript typescript-generics