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

fix: add guards for common Next.js App Router mistakes #499

Merged
merged 1 commit into from
Jan 26, 2024

Conversation

stipsan
Copy link
Member

@stipsan stipsan commented Jan 26, 2024

Common mistakes such as this:

function getPosts(): Promise<Post[]> {
  const client = createClient({})
  return client.fetch<Post[]>('*[_type == "post"]', {cache: 'no-store'})
}

will now throw an error:

Type 'unknown' is not assignable to type 'Promise<Post[]>'.ts(2322)

Hovering over fetch, which renders with a strikethrough, gives more context:
image

These checks only happen at compile time, in TypeScript, there are no errors or logs happening at runtime to avoid risking introducing a breaking change.

There are two options for resolving the TS error.

a) Move the argument to the third parameter

-  return client.fetch<Post[]>('*[_type == "post"]', {cache: 'no-store'})
+  return client.fetch<Post[]>('*[_type == "post"]', {}, {cache: 'no-store'})

b) Add a query params generic that allows the param

-  return client.fetch<Post[]>('*[_type == "post"]', {cache: 'no-store'})
+  return client.fetch<Post[], {cache: string}>('*[_type == "post"]', {cache: 'no-store'})

Copy link

socket-security bot commented Jan 26, 2024

New dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/next@14.1.0 environment, filesystem, network, shell, unsafe +27 1.12 GB vercel-release-bot

View full report↗︎

@stipsan stipsan force-pushed the add-next.js-common-mistake-type-guard branch 6 times, most recently from 7aed722 to f118855 Compare January 26, 2024 19:02
@stipsan stipsan force-pushed the add-next.js-common-mistake-type-guard branch from f118855 to 1ef2be1 Compare January 26, 2024 19:40
@stipsan stipsan merged commit 323ca33 into main Jan 26, 2024
15 checks passed
@stipsan stipsan deleted the add-next.js-common-mistake-type-guard branch January 26, 2024 19:48
This was referenced Jan 26, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant