upstash/ratelimit-js

Redis object got conflicted

Closed this issue · 21 comments

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",

Hi @hckhanh,

Can you try using the latest Upstash Redis release? we bumped Upstash Redis which should fix the issue.

Hi @CahidArda, thank you.

It's fixed now.

@CahidArda I got another error:

src/lib/upstash.ts:19:3 - error ts(2322): Type 'import("/home/runner/work/sshare/sshare/node_modules/@upstash/redis/cloudflare").Redis' is not assignable to type 'Redis'.
  The types returned by 'multi()' are incompatible between these types.

19   redis: ratelimitRedis,
     ~~~~~

For now, I have to cast to never to make the compilation go through

Do you think that @upstash/redis should be declared as a peer dependency?

Hey @hckhanh, you are right, it should be a peer dependency but it should not cause any problems.

Can you share your ratelimit and redis versions? I couldn't reproduce it with their latest releases

This is my config @ytkimirti:

package.json

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

tsconfig.json

{
  "extends": "astro/tsconfigs/strict",
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "react",
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
  }
}

@hckhanh I still can't reproduce it. Here is the code i tested with. Can you share a codesanbox link? I am not sure what we are doing differently here

import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis/cloudflare";

const redis = Redis.fromEnv();

const ratelimit = new Ratelimit({
  limiter: Ratelimit.fixedWindow(10, "10s"),
  redis: redis,
});

console.log(ratelimit);
dependencies:
@upstash/ratelimit 2.0.4
@upstash/redis 1.34.3
astro 4.16.7

devDependencies:
typescript 5.6.2

@ytkimirti Did you initialize the application with astro and run astro check?

This is my reproduction: https://stackblitz.com/edit/withastro-astro-gdk7od?file=src%2Flib%2Fupstash.ts

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

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,
});

Thank you @ytkimirti

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 @rafalzawadzki, can you share your nextjs and typescript version with us? I couldn't reproduce it

pnpm list next typescript

Hey @rafalzawadzki, can you share your nextjs and typescript version with us? I couldn't reproduce it

pnpm list next typescript

after some prodding it turned out my project was still using the old package version in node_modules/.pnpm. After I deleted it, it magically resolved to the new one.

this never happened to me before so I wonder if it still may be in any way related to your package?

Hmm interesting, but in the error you shared it seems the versions are correct from the file paths. I am still unsure why this happens.

I will probably make a cast inside our sdk to prevent this from happening again

true! maybe it was just my IDE resolving to the wrong directory upon CMD+click in the terminal.

Nevertheless, here are the versions:

next 14.0.4
typescript 5.6.3

Hi @rafalzawadzki and @hckhanh,

do you still experience the issue in ratelimi version 2.0.5?

Let me check again

@CahidArda finally, it is fixed. Thank you for your support.