Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: the Stubify type of DurableObjectNamespace does not correctly handle built-in types like Date. #3620

Open
rxliuli opened this issue Feb 27, 2025 · 0 comments
Assignees
Labels
types Related to @cloudflare/workers-types

Comments

@rxliuli
Copy link

rxliuli commented Feb 27, 2025

I found another issue when using hono-rate-limiter. I encountered this problem when I wanted to test modifications locally.

import { DurableObject } from 'cloudflare:workers'
import { assert, expect, it } from 'vitest'
import { env } from 'cloudflare:test'

type ClientRateLimitInfo = {
  totalHits: number
  resetTime?: Date
}

class MyDurableObject extends DurableObject {
  value() {
    return this.ctx.storage.get<ClientRateLimitInfo>('value')
  }
}

it('should work', async () => {
  const _env = env as {
    MY_DURABLE_OBJECT: DurableObjectNamespace<MyDurableObject>
  }
  const id = _env.MY_DURABLE_OBJECT.idFromName('1')
  const stub = _env.MY_DURABLE_OBJECT.get(id)
  const r = await stub.value()
  assert(r)
  expect<ClientRateLimitInfo>(r)
})

Error

Argument of type '{ totalHits: number; resetTime?: { toString: Stub<() => string>; toDateString: Stub<() => string>; toTimeString: Stub<() => string>; toLocaleString: Stub<{ (): string; (locales?: string | string[] | undefined, options?: DateTimeFormatOptions | undefined): string; (locales?: LocalesArgument, options?: DateTimeFormatO...' is not assignable to parameter of type 'ClientRateLimitInfo'.
  Types of property 'resetTime' are incompatible.
    Type '{ toString: Stub<() => string>; toDateString: Stub<() => string>; toTimeString: Stub<() => string>; toLocaleString: Stub<{ (): string; (locales?: string | string[] | undefined, options?: DateTimeFormatOptions | undefined): string; (locales?: LocalesArgument, options?: DateTimeFormatOptions | undefined): string; }>; ...' is not assignable to type 'Date | undefined'.
      Type '{ toString: Stub<() => string>; toDateString: Stub<() => string>; toTimeString: Stub<() => string>; toLocaleString: Stub<{ (): string; (locales?: string | string[] | undefined, options?: DateTimeFormatOptions | undefined): string; (locales?: LocalesArgument, options?: DateTimeFormatOptions | undefined): string; }>; ...' is not assignable to type 'Date'.
        The types returned by 'toString(...)' are incompatible between these types.
          Type 'Promise<string>' is not assignable to type 'string'.ts(2345)

Modify the implementation of Stubify to correctly ignore the Date type.

type Stubify<T> = T extends Stubable
  ? Stub<T>
  : T extends Map<infer K, infer V>
  ? Map<Stubify<K>, Stubify<V>>
  : T extends Set<infer V>
  ? Set<Stubify<V>>
  : T extends Array<infer V>
  ? Array<Stubify<V>>
  : T extends ReadonlyArray<infer V>
  ? ReadonlyArray<Stubify<V>>
  : T extends Rpc.Serializable // modify
  ? T
  : T extends {
      [key: string | number]: any
    }
  ? {
      [K in keyof T]: Stubify<T[K]>
    }
  : T
@rxliuli rxliuli added the types Related to @cloudflare/workers-types label Feb 27, 2025
@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Feb 27, 2025
rxliuli added a commit to rxliuli/workerd that referenced this issue Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types Related to @cloudflare/workers-types
Projects
Status: Untriaged
Development

No branches or pull requests

2 participants