type-fest icon indicating copy to clipboard operation
type-fest copied to clipboard

Documentation improvement request: Merge versus intersection

Open papb opened this issue 4 years ago • 2 comments

Recently someone pushed this commit to Got with the following message:

Use Merge as it's stricter than the intersection operator

I came here looking for an explanation on what exactly this means, and found none. I noticed that Merge uses the intersection operator under the hood, but I'd like to understand this better. Thanks.

Relates to #8

Also seems very related to this question I asked on SO recently (in which coincidentally I invented the terminology merged type before noticing the existence of Merge here in type-fest)

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

papb avatar Apr 22 '20 12:04 papb

I have the same question 4 years on. @sindresorhus I looked in git blame and it seems that this type has existed since the initial commit to the repository, so I supposed you authored it yourself. Are you able to clarify here what are the differences?

rdsedmundo avatar Feb 23 '24 14:02 rdsedmundo

I figured at least one case myself where there's a difference:

import type { Merge } from 'type-fest';

type TypeA = {
  prop: string;
};

type TypeB = {
  prop: number;
};

type TypesIntersected = TypeA & TypeB;
type TypesMerged = Merge<TypeA, TypeB>;

const test1: TypesIntersected = {
  // @ts-expect-error – this type is resolved to `never`
  prop: 1,
};

const test2: TypesMerged = {
  prop: 1,
};

console.log(test1, test2);

rdsedmundo avatar Feb 23 '24 15:02 rdsedmundo