Skip to content
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

Fix Merge with optional any value #583

Merged
merged 2 commits into from Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);