forked from mcollina/async-cache-dedupe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test-d.ts
49 lines (38 loc) · 1.16 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
// Write a tsd file for the module
import { expectType } from "tsd";
import { createCache, Cache, createStorage } from ".";
import { StorageInterface, StorageMemoryOptions } from "./index.js";
// Testing internal types
const storageOptions: StorageMemoryOptions = {
size: 1000,
};
const cache = createCache();
expectType<Cache>(cache);
const storage = createStorage("memory", storageOptions);
expectType<StorageInterface>(storage);
const memoryCache = createCache({
storage: {
type: "memory",
options: storageOptions,
},
});
expectType<Cache>(memoryCache);
// Testing Union Types
const fetchSomething = async (k: any) => {
console.log("query", k);
return { k };
};
export type CachedFunctions = {
fetchSomething: typeof fetchSomething;
};
const unionMemoryCache = createCache({
storage: {
type: "memory",
options: storageOptions,
},
}) as Cache & CachedFunctions;
expectType<Cache & CachedFunctions>(unionMemoryCache);
unionMemoryCache.define("fetchSomething", fetchSomething);
expectType<typeof fetchSomething>(unionMemoryCache.fetchSomething);
const result = await unionMemoryCache.fetchSomething("test");
expectType<{ k: any }>(result);