ratelimit-js: Redis object got conflicted

I get this error when try to use this package

src/lib/upstash.ts:15:3 - error ts(2322): Type 'import("/Users/khanh/RustRoverProjects/sshare/node_modules/@upstash/redis/nodejs").Redis' is not assignable to type 'Redis'.
  The types returned by 'multi()' are incompatible between these types.

15   redis: ratelimitRedis,
     ~~~~~
import { Ratelimit } from '@upstash/ratelimit'
import { Redis } from '@upstash/redis/cloudflare'
import {
  UPSTASH_RATELIMIT_REDIS_TOKEN,
  UPSTASH_RATELIMIT_REDIS_URL,
} from 'astro:env/server'

export const ratelimitRedis = new Redis({
  token: UPSTASH_RATELIMIT_REDIS_TOKEN,
  url: UPSTASH_RATELIMIT_REDIS_URL,
  enableTelemetry: false,
})

export const verifyPasswordRatelimit = new Ratelimit({
  redis: ratelimitRedis,
  limiter: Ratelimit.slidingWindow(10, '10 s'),
  prefix: 'verify-password',
  analytics: true,
})

package.json

    "@upstash/ratelimit": "2.0.3",
    "@upstash/redis": "1.34.3",

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 21 (6 by maintainers)

Most upvoted comments

The same happened to me in a NextJS project.

@upstash/ratelimit 2.0.4
@upstash/redis 1.34.3
lib/utils/rate-limiter.ts:11:3 - error TS2322: Type 'import("---/node_modules/.pnpm/@upstash+redis@1.34.3/node_modules/@upstash/redis/nodejs").Redis' is not assignable to type 'Redis'.
  The types returned by 'multi()' are incompatible between these types.
    Type 'import("---/node_modules/.pnpm/@upstash+redis@1.34.3/node_modules/@upstash/redis/zmscore-Dc6Llqgr").P<[]>' is not assignable to type 'Pipeline<[]>'.
      Types have separate declarations of a private property 'client'.

11   redis: Redis.fromEnv(),
     ~~~~~

  node_modules/.pnpm/@upstash+ratelimit@2.0.4/node_modules/@upstash/ratelimit/dist/index.d.ts:516:5
    516     redis: Redis;
            ~~~~~
    The expected type comes from property 'redis' which is declared here on type 'RegionRatelimitConfig'

lib/utils/rate-limiter.ts:17:3 - error TS2322: Type 'import("---/node_modules/.pnpm/@upstash+redis@1.34.3/node_modules/@upstash/redis/nodejs").Redis' is not assignable to type 'Redis'.
  The types returned by 'multi()' are incompatible between these types.
    Type 'import("---/node_modules/.pnpm/@upstash+redis@1.34.3/node_modules/@upstash/redis/zmscore-Dc6Llqgr").P<[]>' is not assignable to type 'Pipeline<[]>'.
      Types have separate declarations of a private property 'client'.

17   redis,
     ~~~~~

  node_modules/.pnpm/@upstash+ratelimit@2.0.4/node_modules/@upstash/ratelimit/dist/index.d.ts:516:5
    516     redis: Redis;
            ~~~~~
    The expected type comes from property 'redis' which is declared here on type 'RegionRatelimitConfig'

Hey @hckhanh, I tried a few stuff and I couldn’t find anything wrong with how we use peer dependencies. I opened an issue in the astro repo.

Meanwhile, a simple // @ts-ignore should be enough

export const ratelimit = new Ratelimit({
  // @ts-ignore
  redis: redis,
  limiter: Ratelimit.slidingWindow(10, "1 m"),
  enableProtection: true,
  analytics: true,
});

Yes you are right, thanks a lot for reproduction repo.

It’s weird that it only happens with astro-check. I will look into it today