-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
70 lines (61 loc) · 2.03 KB
/
index.test.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
import { TEventEmitter } from '.'
const emitter = new TEventEmitter()
export type Expect<T extends true> = T
export type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? true : false
export type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true
const e1 = emitter.createKey<[string, number, { foo: string }]>('e1')
emitter.on(e1, (a1, a2, a3) => {
type cases = [
Expect<NotEqual<typeof a1, any>>,
Expect<NotEqual<typeof a2, any>>,
Expect<NotEqual<typeof a3, any>>,
Expect<Equal<string, typeof a1>>,
Expect<Equal<number, typeof a2>>,
Expect<Equal<{ foo: string }, typeof a3>>,
]
})
emitter.once(e1, (a1, a2, a3) => {
type cases = [
Expect<NotEqual<typeof a1, any>>,
Expect<NotEqual<typeof a2, any>>,
Expect<NotEqual<typeof a3, any>>,
Expect<Equal<string, typeof a1>>,
Expect<Equal<number, typeof a2>>,
Expect<Equal<{ foo: string }, typeof a3>>,
]
})
emitter.addListener(e1, (a1, a2, a3) => {
type cases = [
Expect<NotEqual<typeof a1, any>>,
Expect<NotEqual<typeof a2, any>>,
Expect<NotEqual<typeof a3, any>>,
Expect<Equal<string, typeof a1>>,
Expect<Equal<number, typeof a2>>,
Expect<Equal<{ foo: string }, typeof a3>>,
]
})
emitter.removeListener(e1, (a1, a2, a3) => {
type cases = [
Expect<NotEqual<typeof a1, any>>,
Expect<NotEqual<typeof a2, any>>,
Expect<NotEqual<typeof a3, any>>,
Expect<Equal<string, typeof a1>>,
Expect<Equal<number, typeof a2>>,
Expect<Equal<{ foo: string }, typeof a3>>,
]
})
emitter.off(e1, (a1, a2, a3) => {
type cases = [
Expect<NotEqual<typeof a1, any>>,
Expect<NotEqual<typeof a2, any>>,
Expect<NotEqual<typeof a3, any>>,
Expect<Equal<string, typeof a1>>,
Expect<Equal<number, typeof a2>>,
Expect<Equal<{ foo: string }, typeof a3>>,
]
})
type ExpectArgTypes = [string, number]
type ArgTypes = typeof emitter.emit<ExpectArgTypes> extends (eventName: any, ...args: infer U) => boolean ? U : never
type ArgTypeCase = Expect<Equal<ExpectArgTypes, ArgTypes>>