-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.ts
More file actions
62 lines (55 loc) · 1.47 KB
/
Copy pathexample.ts
File metadata and controls
62 lines (55 loc) · 1.47 KB
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
import { Bottle } from './src/bottle.ts';
import { service } from './src/service.ts';
class Water {}
class Barley {
constructor(public water: Water) {}
}
class Hops {
constructor(public water: Water) {}
}
class Beer {
constructor(
public name: string,
public barley: Barley,
public hops: Hops,
public water: Water,
) {}
}
const someProviders = {
barley: ({ water }: { water: Water }) => new Barley(water),
// It is possible to use the alternative `service` helper
// hops: ({ water }: { water: Water }) => new Hops(water),
hops: service(Hops, ['water'] as const),
water: () => new Water(),
};
const someBottle = new Bottle(someProviders);
// inferred type
type SomeServices = typeof someBottle.container;
// type SomeServices = {
// barley: Barley;
// hops: Hops;
// water: Water;
// }
const someOtherProviders = {
now: () => Date.now,
beer: (
{ barley, hops, water }: { barley: Barley; hops: Hops; water: Water },
) => new Beer('San Miguel', barley, hops, water),
};
const otherBottle = new Bottle(someOtherProviders, someBottle);
// inferred type
type OtherServices = typeof otherBottle.container;
// type OtherServices = {
// now: () => number;
// beer: Beer;
// barley: Barley;
// hops: Hops;
// water: Water;
// }
console.log(otherBottle.container.beer.name);
// "San Miguel"
console.log(
someBottle.container.water ===
otherBottle.container.beer.water,
);
// true