diff --git a/src/utils/index.ts b/src/utils/index.ts index c699dc1bf..d2c165277 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -52,17 +52,28 @@ export function each(obj: any, iterator: (value: any, key: any) => void | Breake } } -export const extend = function (obj: Record, ...args: Record[]): Record { +export const extend = function , U extends Record[]>( + obj: T, + ...args: U +): ExtendTypeHelper { eachArray(args, function (source) { for (const prop in source) { if (source[prop] !== void 0) { + // @ts-expect-error we're being a bit too clever in exchange for having a simple type for this function obj[prop] = source[prop] } } }) - return obj + return obj as ExtendTypeHelper } +// overwrite the keys in an object, always taking the latest value +type ExtendTypeHelper = U extends [infer First, ...infer Rest] + ? Rest extends Record[] + ? ExtendTypeHelper & First, Rest> + : T + : T + export const include = function ( obj: null | string | Array | Record, target: any