Skip to content

fix(26058): add MergeTuple for Object.assign's return type #45731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 18 additions & 29 deletions lib/lib.es2015.core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,42 +276,31 @@ interface NumberConstructor {
parseInt(string: string, radix?: number): number;
}

interface ObjectConstructor {
/**
* Copy the values of all of the enumerable own properties from one or more source objects to a
* target object. Returns the target object.
* @param target The target object to copy to.
* @param source The source object from which to copy properties.
*/
assign<T, U>(target: T, source: U): T & U;

/**
* Copy the values of all of the enumerable own properties from one or more source objects to a
* target object. Returns the target object.
* @param target The target object to copy to.
* @param source1 The first source object from which to copy properties.
* @param source2 The second source object from which to copy properties.
*/
assign<T, U, V>(target: T, source1: U, source2: V): T & U & V;

/**
* Copy the values of all of the enumerable own properties from one or more source objects to a
* target object. Returns the target object.
* @param target The target object to copy to.
* @param source1 The first source object from which to copy properties.
* @param source2 The second source object from which to copy properties.
* @param source3 The third source object from which to copy properties.
*/
assign<T, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W;
/**
* Return a empty object type if the param's type is undefined
*/
type Undefined2Object<T> = T extends undefined ? {} : T;

/**
* Remove the first two item of tuple
*/
type RemoveFirstTwo<T extends unknown[], First, Second> = T extends [a: First, b: Second, ...rest: infer P] ? P : [];

/**
* Convert tuple to intersection with tuple item's type
*/
type MergeTuple<T extends unknown[]> = T[2] extends undefined
? Undefined2Object<T[0]> & Undefined2Object<T[1]>
: MergeTuple<[T[0], T[1]]> & MergeTuple<RemoveFirstTwo<T, T[0], T[1]>>;

interface ObjectConstructor {
/**
* Copy the values of all of the enumerable own properties from one or more source objects to a
* target object. Returns the target object.
* @param target The target object to copy to.
* @param sources One or more source objects from which to copy properties
*/
assign(target: object, ...sources: any[]): any;

assign<T extends object, R extends object[]>(target: T, ...sources: R): MergeTuple<[T, ...R]>;
/**
* Returns an array of all symbol properties found directly on object o.
* @param o Object to retrieve the symbols from.
Expand Down