-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathmutate.ts
118 lines (108 loc) · 2.84 KB
/
mutate.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
import type { Equal, Expect } from '@type-challenges/utils'
import useSWR, { useSWRConfig } from 'swr'
import type {
MutatorFn,
Key,
MutatorCallback,
Mutator,
MutatorWrapper,
Arguments
} from 'swr/_internal'
import { expectType } from './utils'
type Case1<Data = any> = MutatorFn<Data>
type Case2<Data = any> = (
cache: Cache,
key: Key,
data: Data | Promise<Data> | MutatorCallback<Data>,
opts: boolean
) => Promise<Data | undefined>
type Case3<Data = any> = (
cache: Cache,
key: Key,
data: Data | Promise<Data> | MutatorCallback<Data>
) => Promise<Data | undefined>
type Case4<Data = any> = (
cache: Cache,
key: Key,
data: Data | Promise<Data> | MutatorCallback<Data>,
opts: {
populateCache: undefined
}
) => Promise<Data | undefined>
type Case5<Data = any> = (
cache: Cache,
key: Key,
data: Data | Promise<Data> | MutatorCallback<Data>,
opts: {
populateCache: false
}
) => Promise<Data | undefined>
type Case6<Data = any> = (
cache: Cache,
key: Key,
data: Data | Promise<Data> | MutatorCallback<Data>,
opts: {
populateCache: true
}
) => Promise<Data | undefined>
export type TestCasesForMutator = [
Expect<Equal<Mutator<{}>, Promise<{} | undefined>>>,
Expect<Equal<MutatorWrapper<Case1<{}>>, Promise<{} | undefined>>>,
Expect<Equal<MutatorWrapper<Case2<{}>>, Promise<{} | undefined>>>,
Expect<Equal<MutatorWrapper<Case3<{}>>, Promise<{} | undefined>>>,
Expect<Equal<MutatorWrapper<Case4<{}>>, Promise<{} | undefined>>>,
Expect<Equal<MutatorWrapper<Case5<{}>>, never>>,
Expect<Equal<MutatorWrapper<Case6<{}>>, Promise<{} | undefined>>>
]
export function useMutatorTypes() {
const { mutate } = useSWR<string>('')
mutate(async () => '1')
mutate(async () => '1', { populateCache: false })
// @ts-expect-error
mutate(async () => 1)
// @ts-expect-error
mutate(async () => 1, { populateCache: false })
}
export function useConfigMutate() {
const { mutate } = useSWRConfig()
expect<Promise<Array<any>>>(
mutate(
key => {
expectType<Arguments>(key)
return typeof key === 'string' && key.startsWith('swr')
},
data => {
expectType<number | undefined>(data)
return 0
}
)
)
expect<Promise<any>>(
mutate('string', (data?: string) => {
expectType<string | undefined>(data)
return '0'
})
)
expect<Promise<Array<number | undefined>>>(
mutate<number>(
key => {
expectType<Arguments>(key)
return typeof key === 'string' && key.startsWith('swr')
},
data => {
expectType<number | undefined>(data)
return 0
}
)
)
expect<Promise<string | undefined>>(
mutate<string>('string', data => {
expectType<string | undefined>(data)
return '0'
})
)
mutate<string>('string', data => {
expectType<string | undefined>(data)
return '0'
})
}