t-comm
Version:
专业、稳定、纯粹的工具库
33 lines (32 loc) • 1.17 kB
TypeScript
/**
* 高度可配置的参数获取函数。
* 它根据提供的参数名列表,智能地将分散参数或对象参数转换为一个标准化的对象。
*
* @template T - 期望返回的对象类型。
* @param {string[]} paramNames - 参数名列表,用于映射分散的参数。
* @param {any[]} args - 原始的函数参数 `arguments`。
* @returns {T} - 一个标准化的对象。
* @example
* ```ts
* // 1. 分散参数:按 paramNames 顺序映射
* function foo(a: string, b: number, c: boolean) {
* return getParameters<{ a: string; b: number; c: boolean }>(
* ['a', 'b', 'c'],
* // eslint-disable-next-line prefer-rest-params
* [...arguments],
* );
* }
* foo('x', 1, true); // { a: 'x', b: 1, c: true }
*
* // 2. 对象参数:原样返回
* function bar(opts: { a: string; b: number }) {
* return getParameters<{ a: string; b: number }>(
* ['a', 'b'],
* // eslint-disable-next-line prefer-rest-params
* [...arguments],
* );
* }
* bar({ a: 'x', b: 1 }); // { a: 'x', b: 1 }
* ```
*/
export declare function getParameters<T>(paramNames: string[] | readonly string[], args?: any[]): T;