Hey all, I've had an issue bugging me

Hey all, I've had an issue bugging me for a while, and I can't figure out how to fix it. Looking for help 🙏 I've been developing oss libraries to use with the cloudflare ecosystem, and I've always run into issues with KV typing. Let me give an example. Here is a library that provides a kv adapter for epic-web/cachified - https://www.npmjs.com/package/cachified-adapter-cloudflare-kv Here is my super simplified example setup with it
import { Cache as CachifiedCache } from '@epic-web/cachified';
import { cloudflareKvCacheAdapter } from 'cachified-adapter-cloudflare-kv';

export type Env = {
CACHIFIED_CACHE: KVNamespace;
CACHIFIED_CACHE_INTERFACE: CachifiedCache;
};
export default {
fetch(request: Request, env: Env, _ctx: ExecutionContext) {
env.CACHIFIED_CACHE_INTERFACE = cloudflareKvCacheAdapter({
// type error here 😭
kv: env.CACHIFIED_CACHE,
});
return new Response('Hello, world!', { status: 200 });
},
};
import { Cache as CachifiedCache } from '@epic-web/cachified';
import { cloudflareKvCacheAdapter } from 'cachified-adapter-cloudflare-kv';

export type Env = {
CACHIFIED_CACHE: KVNamespace;
CACHIFIED_CACHE_INTERFACE: CachifiedCache;
};
export default {
fetch(request: Request, env: Env, _ctx: ExecutionContext) {
env.CACHIFIED_CACHE_INTERFACE = cloudflareKvCacheAdapter({
// type error here 😭
kv: env.CACHIFIED_CACHE,
});
return new Response('Hello, world!', { status: 200 });
},
};
Tldr, I've never been able to get the KVNamespace type used inside the library to match the KVNamespace type used in the worker. We see the following error
> tsc --noEmit

src/test.ts:11:7 - error TS2322: Type 'KVNamespace<string>' is not assignable to type 'import("/Users/arishi/personal/my-monorepo/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workers-types/index").KVNamespace<string>'.

11 kv: env.CACHIFIED_CACHE,
~~

../../node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]/node_modules/cachified-adapter-cloudflare-kv/dist/src/index.d.ts:10:5
10 kv: KVNamespace;
~~
The expected type comes from property 'kv' which is declared here on type 'CloudflareKvCacheConfig'


Found 1 error in src/test.ts:11
> tsc --noEmit

src/test.ts:11:7 - error TS2322: Type 'KVNamespace<string>' is not assignable to type 'import("/Users/arishi/personal/my-monorepo/node_modules/.pnpm/@[email protected]/node_modules/@cloudflare/workers-types/index").KVNamespace<string>'.

11 kv: env.CACHIFIED_CACHE,
~~

../../node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]/node_modules/cachified-adapter-cloudflare-kv/dist/src/index.d.ts:10:5
10 kv: KVNamespace;
~~
The expected type comes from property 'kv' which is declared here on type 'CloudflareKvCacheConfig'


Found 1 error in src/test.ts:11
I know it's not a super simple ask, but I would greatly appreciate any help or tips.
3 Replies
Adi
AdiOP10mo ago
Made a thread for this so as to not pollute the main chat. Happy to give any additional context needed
DaniFoldi
DaniFoldi10mo ago
Hey @Adi I think what you're running into is a different entrypoint being used in the library vs your code. Looking at the source of cachified-adapter-cloudflare-kv it looks like it's using the oldest date:
import type { KVNamespace } from "@cloudflare/workers-types"
import type { KVNamespace } from "@cloudflare/workers-types"
And quite likely, you are (may I add, correctly, and as suggested by the docs) using 2023-07-01, or some other dated entrypoint, corresponding to your worker compatibility date. These two definitions, while identical, create a conflict in TS that results in an error. As for a solution, I usually patch all libraries importing from workers-types/declaring dependency with a /// comment to synchronize the entrypoint they use, avoiding this error.
Adi
AdiOP10mo ago
Oooh I see! I didn't even consider that. I'll look down this angle. Just curious, what's that /// command you use to sync types? So for someone developing a library. I assume the recommendation is to import the latest type within the library, and then add docs to explain how users can sync types using your /// command?
Want results from more Discord servers?
Add your server