sindresorhus / type-fest

A collection of essential TypeScript types
Creative Commons Zero v1.0 Universal
14.42k stars 548 forks source link

Documentation improvement request: Merge versus intersection #101

Open papb opened 4 years ago

papb commented 4 years ago

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

Fund with Polar

rdsedmundo commented 9 months ago

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 commented 9 months ago

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);