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

ReadonlyURLSearchParams is incompatible with URLSearchParams #49774

Closed
1 task done
laozhu opened this issue May 14, 2023 · 3 comments · Fixed by #53144
Closed
1 task done

ReadonlyURLSearchParams is incompatible with URLSearchParams #49774

laozhu opened this issue May 14, 2023 · 3 comments · Fixed by #53144
Labels
bug Issue was opened via the bug report template. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@laozhu
Copy link

laozhu commented May 14, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000
    Binaries:
      Node: 19.8.1
      npm: 9.5.1
      Yarn: 1.22.19
      pnpm: 8.5.0
    Relevant packages:
      next: 13.4.2
      eslint-config-next: 13.4.2
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.0.4

warn  - Latest canary version not detected, detected: "13.4.2", newest: "13.4.3-canary.0".
        Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.
        Read more - https://nextjs.org/docs/messages/opening-an-issue

Which area(s) of Next.js are affected? (leave empty if unsure)

Routing (next/router, next/navigation, next/link)

Link to the code that reproduces this issue

no

To Reproduce

import { usePathname, useRouter, useSearchParams } from "next/navigation";

const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();

const clonedSearchParams = new URLSearchParams(searchParams);
clonedSearchParams.sort();

router.replace(["/login", clonedSearchParams.toString()].filter(Boolean).join("?"));

Describe the Bug

Got type error

Argument of type 'ReadonlyURLSearchParams' is not assignable to parameter of type 'string | URLSearchParams | string[][] | Record<string, string> | undefined'.
Property 'size' is missing in type 'ReadonlyURLSearchParams' but required in type 'URLSearchParams'.ts(2345)
url.d.ts(840, 18): 'size' is declared here.

Expected Behavior

No type error

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@laozhu laozhu added the bug Issue was opened via the bug report template. label May 14, 2023
@github-actions github-actions bot added the Navigation Related to Next.js linking (e.g., <Link>) and navigation. label May 14, 2023
@Naddiseo
Copy link

Naddiseo commented Jun 6, 2023

According to @types/node the size parameter was added to URLSearchParams in v18.16

@erikschul
Copy link

erikschul commented Jul 1, 2023

Related docs that are broken: https://nextjs.org/docs/app/api-reference/functions/use-search-params#updating-searchparams

Trivial fix: const params = new URLSearchParams(Array.from(searchParams.entries()));

@kodiakhq kodiakhq bot closed this as completed in #53144 Aug 17, 2023
kodiakhq bot pushed a commit that referenced this issue Aug 17, 2023
Closes #49774 (and my dupe #53141), where there's a type error when attempting to construct URLSearchParams from ReadonlyURLSearchParams, [as recommended in the Next docs here](https://nextjs.org/docs/app/api-reference/functions/use-search-params#updating-searchparams) and breaks `next build` on Node v18.16.0+, including on Vercel.


This is blocked by TS publishing the new `dom` types (microsoft/TypeScript#54466). The [URL Standard](https://url.spec.whatwg.org/#dom-urlsearchparams-size) added the size property to URLSearchParams (whatwg/url#734) and this now has wide [browser support](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/size).
@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2023

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot added the locked label Sep 1, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants