-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
mutator.ts
69 lines (62 loc) · 1.75 KB
/
mutator.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
import { Equal, Expect } from '@type-challenges/utils'
import useSWR from 'swr'
import {
MutatorFn,
Key,
MutatorCallback,
Mutator,
MutatorWrapper
} from '../../_internal/types'
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')
// @ts-expect-error
mutate(async () => 1)
// FIXME: this should work.
// mutate(async () => 1, { populateCache: false })
}