-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.5.spec.ts
79 lines (72 loc) · 2.21 KB
/
3.5.spec.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// https://devblogs.microsoft.com/typescript/announcing-typescript-3-5/
describe('3.5 https://devblogs.microsoft.com/typescript/announcing-typescript-3-5/', () => {
describe('smarter union types', () => {
interface S {
done: boolean;
value: number;
}
type T = { done: false; value: number } | { done: true; value: number };
it('should allow assigning T to S by decomposing S into every combination', () => {
const isNotDone: T = { done: false, value: 1 };
const moreGeneral: S = isNotDone;
expect(moreGeneral.done).toEqual(isNotDone.done);
});
});
describe('Omit helper to streamline pick & exclude usage', () => {
interface WitnessRelocation {
name: string;
age: number;
relocation: string;
}
// previous way
type WhitelistedKeys = Exclude<keyof Witness, 'relocation'>;
type PersonOfInterest = Pick<Witness, WhitelistedKeys>;
// Omit
type Witness = Omit<WitnessRelocation, 'relocation'>;
it('should omit a property', () => {
const witness: WitnessRelocation = {
name: 'William',
age: 92,
relocation: 'Austin',
};
expect(witness.relocation).toEqual('Austin');
const omitPerson: Witness = {
name: 'Bill',
age: 29,
};
const pickExcludePerson: PersonOfInterest = {
name: 'Bill',
age: 29,
};
expect(omitPerson.age).toEqual(pickExcludePerson.age);
});
});
describe('Higher order type inference from generic constructors', () => {
class Box<T> {
public kind: 'box';
public value: T;
constructor(value: T) {
this.value = value;
}
}
class Bag<U> {
public kind: 'bag';
public value: U;
constructor(value: U) {
this.value = value;
}
}
function composeCtor<T, U, V>(
F: new (x: T) => U,
G: new (y: U) => V
): (x: T) => V {
return (x: T): V => new G(new F(x));
}
it('should compose generic constructor functions', () => {
const f: <T>(x: T) => Bag<Box<T>> = composeCtor(Box, Bag);
const bagBox: Bag<Box<number>> = f(1024);
const box: Box<number> = bagBox.value;
expect(box.value).toEqual(1024);
});
});
});