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

MergeDeep takes type of property of source instead of using the destination type in case of undefined value #941

Open
Robstei opened this issue Aug 14, 2024 · 1 comment
Labels
bug Something isn't working question Further information is requested

Comments

@Robstei
Copy link

Robstei commented Aug 14, 2024

I am trying out remeda and came around the MergeDeep type when using the mergeDeep function from remeda.

My problem is that the MergeDeep type always uses the property type of the source value instead of using the value of the destination incase the source value is undefined.

Does this work as intended or is this a bug?

stackblitz

import { MergeDeep } from 'type-fest';

type Person = { name: string; age: number };

type PartialPerson = Partial<Person>;

// type of MergeDeepPersonPartialFirst makes sense, since all Person values will be used
type MergeDeepPersonPartialFirst = MergeDeep<PartialPerson, Person>;

// I assumed this would be inferred as the same type as MergeDeepPersonPartialLast
// but it gets infered as
// {
//   name?: string | undefined;
//   age?: number | undefined;
// }
type MergeDeepPersonPartialLast = MergeDeep<Person, PartialPerson>;

const person: Person = { name: 'a', age: 25 };
const partialPerson: PartialPerson = { name: 'b' };

// both values get inferred as
// {
//    name: string;
//    age: number;
// }
// equivalent to Person
const spreadPartialLast = { ...person, ...partialPerson };
const spreadPartiaFirst = { ...partialPerson, ...person };

// on a sideonde
// these get infered as string
type test1 = string & (string | undefined);
type test2 = (string | undefined) & string;

// these get infered as something equivalent to Person
type test3 = Partial<Person> & Person;
type test4 = Person & Partial<Person>;

Thanks for the your time

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@fregante fregante added the question Further information is requested label Aug 17, 2024
@Emiyaaaaa
Copy link
Collaborator

I think this is a bug

@Emiyaaaaa Emiyaaaaa added the bug Something isn't working label Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants