cloudflare / workers-types

TypeScript type definitions for authoring Cloudflare Workers.
BSD 3-Clause "New" or "Revised" License
359 stars 89 forks source link

Accept Metadata type argument on KVNamespace #292

Closed tavoyne closed 1 year ago

tavoyne commented 2 years ago

Currently, the shape of the metadata object can only be provided when calling the env.*.getWithMetadata<Metadata>() method, through the first (and only) type argument.

Multiple call of this method => Metadata has to be passed multiple times => not DRY.

Most of the time, we know the shape of this object right where we define the KVNamespace type. I propose that we are able to pass Metadata to KVNamespace as a the second type argument (after K), e.g.:

interface Env {
  SESSIONS: KVNamespace<string, { emailAddress: string }>;
  USERS: KVNamespace;
}

// We can still do that:
env.SESSIONS.getWithMetadata<Metadata>(sessionId);

This (micro) change is backward-compatible.

changeset-bot[bot] commented 2 years ago

⚠️ No Changeset found

Latest commit: 1957162fae20e5a18ce7e45f1840a15edb23305d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR