Skip to content

Commit

Permalink
Feat: Add up to 50 generic type parameters to XOR (#389)
Browse files Browse the repository at this point in the history
* feat: βž• add up to 50 generic types in XOR

* test: πŸ§ͺ XOR

* docs: πŸ“„ XOR

* test: πŸ§ͺ variadic xor

* docs: πŸ“„ tests check in contributing

* docs: πŸ“„ changeset
  • Loading branch information
Beraliv committed Apr 27, 2024
1 parent 9935d80 commit 5b7650a
Show file tree
Hide file tree
Showing 7 changed files with 3,011 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-brooms-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ts-essentials": minor
---

Add variadic XOR, up to 50 generic types
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ When you're done with your changes:

- use `yarn test:fix` to run `prettier` to reformat code

- use `tsc` to make sure that there are no compilation errors
- use `yarn test` to make sure that there are no compilation errors

- use `yarn changeset` to create changelog with meaningful description as it will be visible in Releases, e.g.

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ npm install --save-dev ts-essentials
- [`UnionToIntersection<Union>`](/lib/union-to-intersection) - Constructs a intersection type from union type `Union`
- [`ValueOf<Type>`](/lib/value-of) - Constructs a type for type `Type` and equals to a primitive for primitives, array
elements for arrays, function return type for functions or object property values for objects
- [`XOR<Type1, Type2>`](/lib/xor) - Construct a type which is assignable to either type `Type1` or `Type2` but not both
- [`XOR<Type1, Type2, Type3?, ..., Type50?>`](/lib/xor) - Construct a type which is assignable to either type `Type1`,
`Type2` but not both. Starting in ts-essentials@10, it supports up to 50 generic types.

### Mark wrapper types

Expand Down
29 changes: 27 additions & 2 deletions lib/xor/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
`XOR<Type1, Type2>` construct a type which is assignable to either type `Type1` or `Type2` but not both
`XOR<Type1, Type2, Type3?, ..., Type50?>` construct a type which is assignable to either type `Type1` or `Type2` but not
both.

```ts
interface Dog {
Expand Down Expand Up @@ -36,4 +37,28 @@ dogOrCat = { meow };
dogOrCat = { bark, meow };
```

TS Playground – https://tsplay.dev/NV3Dlm
Starting in ts-essentials@10, it supports up to 50 generic types, e.g.

```ts
type SinglePet = XOR<
{ cat: string },
{ dog: string },
{ parrot: string },
{ fish: string },
{ rabbit: string },
{ turtle: string },
{ guineaPig: string },
{ hamster: string }
>;

let singlePet: SinglePet;
singlePet = { cat: "Timofey" };
singlePet = { dog: "Sirius" };
// Type '{ cat: string; dog: string; }' is not assignable to type 'XOR<{ cat: string; }, { dog: string; }, { parrot: string; }, { fish: string; }, { rabbit: string; }, { turtle: string; }, { guineaPig: string; }, { hamster: string; }, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, ... 26 more .....'.
// Type '{ cat: string; dog: string; }' is not assignable to type '{ dog?: undefined; parrot?: undefined; fish?: undefined; rabbit?: undefined; turtle?: undefined; guineaPig?: undefined; hamster?: undefined; cat?: undefined; }'.
// Types of property 'dog' are incompatible.
// Type 'string' is not assignable to type 'undefined'
singlePet = { cat: "Timofey", dog: "Sirius" };
```

TS Playground – https://tsplay.dev/wRb51w

0 comments on commit 5b7650a

Please sign in to comment.