Skip to content

Parameters

返回函数的入参类型

实现

ts
/**
 * Obtain the parameters of a function type in a tuple
 */
type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never

栗子

ts
const func = (a: number, b: number = 0) => a + b
const func1 = (a: number) => a
const func2 = () => 1

type ParametersResult = Parameters<typeof func> // [number , number?]
type ParametersResult1 = Parameters<typeof func1> // [number]
type ParametersResult2 = Parameters<typeof func2> // []