Skip to content

Commit

Permalink
Merge: Don't turn undefined into optional key (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emiyaaaaa authored Jan 2, 2024
1 parent 02a95c5 commit 0aec247
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 5 additions & 3 deletions source/merge.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {OmitIndexSignature} from './omit-index-signature';
import type {PickIndexSignature} from './pick-index-signature';
import type {EnforceOptional} from './enforce-optional';
import type {Simplify} from './simplify';

// Merges two objects without worrying about index signatures.
type SimpleMerge<Destination, Source> = {
Expand Down Expand Up @@ -41,6 +41,8 @@ export type FooBar = Merge<Foo, Bar>;
@category Object
*/
export type Merge<Destination, Source> = EnforceOptional<
export type Merge<Destination, Source> =
Simplify<
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
& SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>>;
& SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
>;
20 changes: 18 additions & 2 deletions test-d/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ expectType<{
c: undefined;
a?: string;
d?: string;
e?: number;
f?: number;
e: number | undefined;
f: number | undefined;
g: undefined;
}>(fooBarWithOptionalKeys);

Expand Down Expand Up @@ -148,3 +148,19 @@ expectType<{
foo: true;
bar?: any;
}>(sourceWithAny);

// Test for issue https://github.com/sindresorhus/type-fest/issues/601
type Baz = {
t1?: number;
t2?: number;
t3?: number;
t4?: number;
};
declare const baz: Merge<Pick<Baz, 't2' | 't4'>, {
list: string[];
}>;
expectType<{
t2?: number;
t4?: number;
list: string[];
}>(baz);

0 comments on commit 0aec247

Please sign in to comment.