forked from proposal-signals/signal-polyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic-api-types.ts
111 lines (103 loc) · 3.44 KB
/
public-api-types.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
/**
* The purpose of these tests is to make sure the types are exposed how we expect,
* and that we double-check what we're exposing as public API
*/
import {expectTypeOf} from 'expect-type';
import {Signal} from './wrapper.ts';
/**
* Top-Level
*/
expectTypeOf<keyof typeof Signal>().toEqualTypeOf<
'State' | 'Computed' | 'subtle' | 'isState' | 'isComputed' | 'isWatcher'
>();
/**
* Construction works as expected
*/
expectTypeOf(Signal.State<number>).toBeConstructibleWith(1);
expectTypeOf(Signal.State<number>).toBeConstructibleWith(1, {});
expectTypeOf(Signal.State<number>).toBeConstructibleWith(1, {equals: (a, b) => true});
expectTypeOf(Signal.State<number>).toBeConstructibleWith(1, {[Signal.subtle.watched]: () => true});
expectTypeOf(Signal.State<number>).toBeConstructibleWith(1, {
[Signal.subtle.unwatched]: () => true,
});
expectTypeOf(Signal.Computed<number>).toBeConstructibleWith(() => 2);
expectTypeOf(Signal.Computed<number>).toBeConstructibleWith(() => 1, {equals: (a, b) => true});
expectTypeOf(Signal.Computed<number>).toBeConstructibleWith(() => 1, {
[Signal.subtle.watched]: () => true,
});
expectTypeOf(Signal.Computed<number>).toBeConstructibleWith(() => 1, {
[Signal.subtle.unwatched]: () => true,
});
// @ts-expect-error
expectTypeOf<Signal.State<number>>().toBeConstructibleWith();
// @ts-expect-error
expectTypeOf(Signal.State<number>).toBeConstructibleWith('wrong', {});
expectTypeOf(Signal.State<number>).toBeConstructibleWith(1, {
// @ts-expect-error
[Signal.subtle.watched]: 2,
});
expectTypeOf(Signal.State<number>).toBeConstructibleWith(1, {
// @ts-expect-error
[Signal.subtle.unwatched]: 2,
});
expectTypeOf(Signal.State<number>).toBeConstructibleWith(1, {
// @ts-expect-error
typo: (a, b) => true,
});
// @ts-expect-error
expectTypeOf<Signal.Computed<number>>().toBeConstructibleWith();
// @ts-expect-error
expectTypeOf(Signal.Computed<number>).toBeConstructibleWith('wrong');
// @ts-expect-error
expectTypeOf(Signal.Computed<number>).toBeConstructibleWith(2);
expectTypeOf(Signal.Computed<number>).toBeConstructibleWith(() => 1, {
// @ts-expect-error
[Signal.subtle.watched]: 2,
});
expectTypeOf(Signal.Computed<number>).toBeConstructibleWith(() => 1, {
// @ts-expect-error
[Signal.subtle.unwatched]: 2,
});
expectTypeOf(Signal.Computed<number>).toBeConstructibleWith(() => 1, {
// @ts-expect-error
typo: (a, b) => true,
});
/**
* Properties on each of the instances / namespaces
*/
expectTypeOf<keyof Signal.State<unknown> & string>().toEqualTypeOf<'get' | 'set'>();
expectTypeOf<keyof Signal.Computed<unknown> & string>().toEqualTypeOf<'get'>();
expectTypeOf<keyof typeof Signal.subtle>().toEqualTypeOf<
| 'untrack'
| 'currentComputed'
| 'introspectSources'
| 'introspectSinks'
| 'hasSinks'
| 'hasSources'
| 'Watcher'
| 'watched'
| 'unwatched'
>();
expectTypeOf<keyof Signal.subtle.Watcher & string>().toEqualTypeOf<
'watch' | 'unwatch' | 'getPending'
>();
/**
* Inference works
*/
expectTypeOf(new Signal.State(0)).toEqualTypeOf<Signal.State<number>>();
expectTypeOf(new Signal.State(0).get()).toEqualTypeOf<number>();
expectTypeOf(new Signal.State(0).set(1)).toEqualTypeOf<void>();
/**
* Assigning subtypes works
*/
expectTypeOf<Signal.Computed<Narrower>>().toMatchTypeOf<Signal.Computed<Broader>>();
expectTypeOf<Signal.State<Narrower>>().toMatchTypeOf<Signal.State<Broader>>();
/**
* Test data types
*/
interface Broader {
strProp: string;
}
interface Narrower extends Broader {
numProp: number;
}