Skip to content

Commit 1e15e93

Browse files
committed
fix: preserve call signatures of target object in Object.assign
1 parent b899fca commit 1e15e93

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

generated/lib.es2015.core.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ interface ObjectConstructor {
328328
assign<T extends {}, Ts extends readonly any[]>(
329329
target: T,
330330
...sources: Ts
331-
): Intersect<[T, ...Ts]>;
331+
): T & Intersect<Ts>;
332332

333333
/**
334334
* Returns an array of all symbol properties found directly on object o.

lib/lib.es2015.core.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ interface ObjectConstructor {
7272
assign<T extends {}, Ts extends readonly any[]>(
7373
target: T,
7474
...sources: Ts
75-
): Intersect<[T, ...Ts]>;
75+
): T & Intersect<Ts>;
7676

7777
/**
7878
* Returns an array of all symbol properties found directly on object o.

tests/src/es2015.core.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,19 @@ import { expectError, expectType } from "tsd";
3434
expectError(Object.getOwnPropertySymbols(null));
3535
const obj5 = Object.setPrototypeOf({ foo: 123 }, { bar: "wow" });
3636
expectType<{ foo: number } & { bar: string }>(obj5);
37+
38+
// https://github.com/uhyo/better-typescript-lib/issues/71
39+
// Object.assign should preserve function callability
40+
const func1 = () => 42;
41+
const funcWithProps = Object.assign(func1, { baz: "hello" });
42+
expectType<(() => number) & { baz: string }>(funcWithProps);
43+
expectType<number>(funcWithProps()); // Should be callable
44+
expectType<string>(funcWithProps.baz); // Should have the added property
45+
46+
const func2 = function(x: number): string { return x.toString(); };
47+
const funcWithProps2 = Object.assign(func2, { value: 123 }, { flag: true });
48+
expectType<((x: number) => string) & { value: number } & { flag: boolean }>(funcWithProps2);
49+
expectType<string>(funcWithProps2(42)); // Should be callable
50+
expectType<number>(funcWithProps2.value); // Should have added properties
51+
expectType<boolean>(funcWithProps2.flag);
3752
}

0 commit comments

Comments
 (0)