-
Notifications
You must be signed in to change notification settings - Fork 0
/
Either.ts
52 lines (51 loc) · 1.96 KB
/
Either.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Never } from "./Never";
/**
* Constructs a type that strictly represents one or the other given types.
*
* The symbol `|` when used among two types is "mentally read" as "or", but it
* isn't. It's name is Union, because it unites together the types that surround
* it.
*
* ```
* type Person = { firstName: string, lastName: string }
* type Company = { legalEntityName: string }
*
* type LoosePersonOrCompany = Person | Company
* ```
*
* In this example `LoosePersonOrCompany` is accepting as a valid value the sum
* (hence, the union) of the `Person` and the `Company` types, not only one of them.
* The following is perfectly valid assignment:
*
* ```typescript
* const O1: LoosePersonOrCompany = {
* firstName: "James",
* lastName: "Bond",
* legalEntityName: "Queen"
* }
* ```
*
* With `Either<Person, Company>`, instead, it complains as it should.
*
* ```typescript
* type StrictPersonOrCompany = Either<Person, Company>
*
* const O2: StrictPersonOrCompany = { firstName: "James", lastName: "Bond", legalEntityName: "Queen" }
* // 😠 Type '{ firstName: string; lastName: string; legalEntityName: string; }' is not assignable to type 'StrictPersonOrCompany'.
*
* const O3: StrictPersonOrCompany = { firstName: "James", legalEntityName: "Queen" }
* // 😠 Type '{ firstName: string; legalEntityName: string; }' is not assignable to type 'StrictPersonOrCompany'.
*
* const O4: StrictPersonOrCompany = { lastName: "Bond", legalEntityName: "Queen" }
* // 😠 Type '{ lastName: string; legalEntityName: string; }' is not assignable to type 'StrictPersonOrCompany'.
*
* const O5: StrictPersonOrCompany = { firstName: "James", lastName: "Bond" }
* // 😎 No problem!
*
* const O6: StrictPersonOrCompany = { legalEntityName: "Queen" }
* // 😎 No problem!
* ```
*/
export type Either<OneType, OtherType> =
| (OneType & Never<Partial<Omit<OtherType, keyof OneType>>>)
| (OtherType & Never<Partial<Omit<OneType, keyof OtherType>>>);