vercel / next.js

The React Framework
https://nextjs.org
MIT License
126.5k stars 26.92k forks source link

Cannot pass an array of tags to `cacheTag` function #71901

Open tomdohnal opened 22 hours ago

tomdohnal commented 22 hours ago

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/wizardly-breeze-j63t72?file=%2Fapp%2Fpage.tsx%3A6%2C3-6%2C32

To Reproduce

  1. open the page.tsx file
  2. hover over the line with cacheTag(["tag-1", "tag-2"]);

Current vs. Expected behavior

I expect to be able to pass an array of tags to the cacheTag function (as per the example given in the docs) but I see an error saying: "Argument of type 'string[]' is not assignable to parameter of type 'string'" instead

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
  Available memory (MB): 4102
  Available CPU cores: 2
Binaries:
  Node: 20.9.0
  npm: 9.8.1
  Yarn: 1.22.19
  pnpm: 8.10.2
Relevant Packages:
  next: 15.0.2-canary.7 // Latest available version is detected (15.0.2-canary.7).
  eslint-config-next: N/A
  react: 19.0.0-rc-1631855f-20241023
  react-dom: 19.0.0-rc-1631855f-20241023
  typescript: 5.3.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

TypeScript

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

As other places in the docs pass multiple tags as separate arguments (rather than as an array) which is the actual type signature of the cacheTag function, I wonder if it's just the case of the docs being wrong here

wiscaksono commented 6 hours ago

The documentation example might be misleading. The cacheTag function uses rest parameters (...tags: string[]), so it expects individual string arguments rather than an array. Here's how to fix it:

// ❌ Incorrect - passing array
cacheTag(["tag-1", "tag-2"]);

// ✅ Correct - passing individual strings
cacheTag("tag-1", "tag-2");

The TypeScript signature is:

function cacheTag(...tags: string[]): void

See the type definition here

This means you can pass any number of string arguments, but they need to be passed as separate arguments, not as an array. The rest parameter syntax (...) will automatically collect them into an array internally. If you have an array of tags that you need to pass, you can spread them:

const tags = ["tag-1", "tag-2"];
cacheTag(...tags);