/** * 将对象类型转换为元组类型(分散参数) * * @template T - 参数对象类型 * @template K - 参数键名数组(按顺序,支持 readonly) * * @example * interface MyParams { * name: string; * age?: number; * city?: string; * } * * // 手动指定顺序 * type MyArgs = ParamsToTuple; * // 结果: [string, number?, string?] */ export type ParamsToTuple = K extends readonly [infer First, ...infer Rest] ? First extends keyof T ? Rest extends readonly (keyof T)[] ? undefined extends T[First] ? [T[First]?, ...ParamsToTuple] : [T[First], ...ParamsToTuple] : [] : [] : []; /** * 创建支持对象参数和分散参数的函数重载类型 * * @template T - 参数对象类型 * @template K - 参数键名数组(按顺序,支持 readonly) * @template R - 返回值类型,默认为 void * * @example * interface ShowDialogParams { * content: string; * title?: string; * } * * type ShowDialogFn = OverloadedFn; * // 等价于: * // { * // (params: ShowDialogParams): void; * // (content: string, title?: string): void; * // } */ export type OverloadedFn = { (params: T): R; (...args: ParamsToTuple): R; }; /** * 函数实现时的参数类型(联合类型) * * @template T - 参数对象类型 * @template K - 参数键名数组(按顺序,支持 readonly) * * @example * type ImplArgs = ImplementationArgs; * // 结果: [ShowDialogParams] | [string, string?] */ export type ImplementationArgs = [T] | ParamsToTuple;