Skip to content

Commit

Permalink
Fix Merge with optional any value (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
skarab42 committed Mar 29, 2023
1 parent 26778c3 commit 60a057b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 3 additions & 7 deletions source/merge.d.ts
Expand Up @@ -2,14 +2,10 @@ import type {OmitIndexSignature} from './omit-index-signature';
import type {PickIndexSignature} from './pick-index-signature';
import type {EnforceOptional} from './enforce-optional';

// Merges two objects without worrying about index signatures or optional keys.
// Merges two objects without worrying about index signatures.
type SimpleMerge<Destination, Source> = {
[Key in keyof Destination | keyof Source]: Key extends keyof Source
? Source[Key]
: Key extends keyof Destination
? Destination[Key]
: never;
};
[Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
} & Source;

/**
Merge two types into a new type. Keys of the second type overrides keys of the first type.
Expand Down
14 changes: 14 additions & 0 deletions test-d/merge.ts
Expand Up @@ -134,3 +134,17 @@ expectType<{
bar: string;
fooBar: string;
}>(fooBarWithIndexSignature);

declare const destinationWithAny: Merge<{foo?: any}, {bar: true}>;

expectType<{
foo?: any;
bar: true;
}>(destinationWithAny);

declare const sourceWithAny: Merge<{foo: true}, {bar?: any}>;

expectType<{
foo: true;
bar?: any;
}>(sourceWithAny);

0 comments on commit 60a057b

Please sign in to comment.