Skip to content

Commit c0665b0

Browse files
committed
Generic type-safe implementation for Object.assign
It seems that it can be possible to make a type-safe `Object.assign`-like functions with TypeScript, without having to make lots of overrides. Here's an example on the playground: typescriptlang.org/play/index.html#src=function%20assign%3CSource%20extends%20%7B%7D%2C%20Result%20extends%20Source%3E(%0D%0A%20%20%20%20target%3A%20Source%2C%0D%0A%20%20%20%20...sources%3A%20Source%5B%5D%0D%0A)%3A%20Pick%3CResult%2C%20keyof%20Source%3E%20%7B%0D%0A%20%20%20%20return%20Object.assign(target%2C%20...sources)%3B%0D%0A%7D%0D%0A%0D%0Alet%20x%20%3D%0D%0A%20%20%20%20assign(%0D%0A%20%20%20%20%20%20%20%20%7B%20hello%3A%20%22world%22%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%20something%3A%20%22is%20good%22%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%20%22yes%22%3A%20true%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%20yes%3A%20%22hello%22%20%7D%0D%0A%20%20%20%20)%3B%0D%0A%0D%0Ax.hello%3B%0D%0Ax.world%3B%20%2F%2F%20error!%0D%0Ax.yes%3B You can see that the compiler says `x.hello` is fine, and complains about `x.world`
1 parent 9e28a81 commit c0665b0

File tree

1 file changed

+2
-29
lines changed

1 file changed

+2
-29
lines changed

src/lib/es2015.core.d.ts

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -261,36 +261,9 @@ interface ObjectConstructor {
261261
* Copy the values of all of the enumerable own properties from one or more source objects to a
262262
* target object. Returns the target object.
263263
* @param target The target object to copy to.
264-
* @param source The source object from which to copy properties.
264+
* @param sources The source objects from which to copy properties.
265265
*/
266-
assign<T, U>(target: T, source: U): T & U;
267-
268-
/**
269-
* Copy the values of all of the enumerable own properties from one or more source objects to a
270-
* target object. Returns the target object.
271-
* @param target The target object to copy to.
272-
* @param source1 The first source object from which to copy properties.
273-
* @param source2 The second source object from which to copy properties.
274-
*/
275-
assign<T, U, V>(target: T, source1: U, source2: V): T & U & V;
276-
277-
/**
278-
* Copy the values of all of the enumerable own properties from one or more source objects to a
279-
* target object. Returns the target object.
280-
* @param target The target object to copy to.
281-
* @param source1 The first source object from which to copy properties.
282-
* @param source2 The second source object from which to copy properties.
283-
* @param source3 The third source object from which to copy properties.
284-
*/
285-
assign<T, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W;
286-
287-
/**
288-
* Copy the values of all of the enumerable own properties from one or more source objects to a
289-
* target object. Returns the target object.
290-
* @param target The target object to copy to.
291-
* @param sources One or more source objects from which to copy properties
292-
*/
293-
assign(target: object, ...sources: any[]): any;
266+
assign<Source extends {}, Result extends Source>(target: Source, ...sources: Source[]): Pick<Result, keyof Source>;
294267

295268
/**
296269
* Returns an array of all symbol properties found directly on object o.

0 commit comments

Comments
 (0)