-
Notifications
You must be signed in to change notification settings - Fork 206
/
index.test-d.ts
160 lines (148 loc) · 4.21 KB
/
index.test-d.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* eslint-disable no-use-before-define, import/no-extraneous-dependencies */
import { expectType } from 'tsd';
import {
defineKind,
defineKindMulti,
makeKindHandle,
defineDurableKind,
partialAssign,
} from '.';
import {
KindFacets,
DurableKindHandle,
KindFacet,
FunctionsPlusContext,
} from './types.js';
/*
export const makePaymentMaker = (allegedName: string, brand: unknown) => {
const makePayment = defineKind(
`${allegedName} payment`,
() => ({}),
// @ts-expect-error "state" type implied by init() doesn't provide "a"
({ a: something }) => ({
getAllegedBrand: () => brand,
}),
);
return makePayment;
};
type FlorgState = { str: string };
const makeFlorg = defineKind(
'florg',
(num: number) => ({ str: String(num), extra: 'extra' }),
({ str }: FlorgState) => str,
);
const f = makeFlorg(42);
f.concat; // string
// @ts-expect-error
makeFlorg('notnumber');
*/
// Single-faceted example from virtual-objects.md
type SingleCounterState = { counter: number; name: string };
type SingleCounterContext = {
state: SingleCounterState;
self: KindFacet<typeof counterBehavior>;
};
const initCounter = (name: string, str: string): SingleCounterState => ({
counter: 0,
name,
});
const counterBehavior = {
inc: ({ state }: SingleCounterContext) => {
state.counter += 1;
},
dec: ({ state }: SingleCounterContext) => {
state.counter -= 1;
},
reset: ({ state }: SingleCounterContext) => {
state.counter = 0;
},
rename: ({ state }: SingleCounterContext, newName: string) => {
state.name = newName;
},
getCount: ({ state }: SingleCounterContext) => state.counter,
getName: ({ state }: SingleCounterContext) => state.name,
};
const finishCounter = ({ state, self }: SingleCounterContext) => {
expectType<string>(state.name);
expectType<number>(self.getCount());
};
const makeCounter = defineKind('counter', initCounter, counterBehavior, {
finish: finishCounter,
});
// Multi-faceted example from virtual-objects.md
type MultiCounterContext = {
state: ReturnType<typeof initFacetedCounter>;
facets: KindFacets<typeof facetedCounterBehavior>;
};
const initFacetedCounter = () => ({ counter: 0 });
const getCount = ({ state }: MultiCounterContext) => state.counter;
const facetedCounterBehavior = {
incr: {
step: ({ state }: MultiCounterContext) => {
state.counter += 1;
},
getCount,
},
decr: {
step: (context: MultiCounterContext) => {
// Destructure within method because doing so in params creates a circular reference
const { state, facets } = context;
const { other } = facets;
other.echo('hi');
state.counter -= 1;
},
getCount,
},
other: {
emptyFn: () => null,
echo: (context: MultiCounterContext, toEcho: string) => toEcho,
},
};
const makeFacetedCounter = defineKindMulti(
'counter',
initFacetedCounter,
facetedCounterBehavior,
);
const fc = makeFacetedCounter();
expectType<void>(fc.incr.step());
expectType<void>(fc.decr.step());
expectType<number>(fc.decr.getCount());
// @ts-expect-error missing argument
fc.decr.echo();
expectType<string>(fc.other.echo('foo'));
// @ts-expect-error missing method
fc.incr.echo('foo');
expectType<null>(fc.other.emptyFn());
// durable kind
const fooHandle = makeKindHandle('foo');
expectType<DurableKindHandle>(fooHandle);
const fooInit = (name: string) => ({ name });
const fooBehavior = {
sayHi: ({ state }: { state: { name: string } }) => `Howdy, ${state.name}`,
};
const makeFoo = defineDurableKind(fooHandle, fooInit, fooBehavior);
const foo = makeFoo('Doody');
expectType<string>(foo.sayHi());
// @ts-expect-error missing method
foo.sayBye();
// partialAssign
const state = { name: 'ted', color: 'red' };
partialAssign(state, { name: 'ed' });
// @ts-expect-error
partialAssign(state, { key: 'ted' });
// @ts-expect-error
partialAssign(state, { name: 3 });
// test FunctionsPlusContext
type SomeFacet = {
gt(b: number): boolean;
};
type SomeContext = { state: { a: number } };
const someBehavior: FunctionsPlusContext<SomeContext, SomeFacet> = {
gt(context: SomeContext, b: number) {
return b > context.state.a;
},
};
const someFacet: KindFacet<typeof someBehavior> = null as any;
// @ts-expect-error
someFacet.gt();
expectType<boolean>(someFacet.gt(1));