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

Next-link props #52929

Closed
1 task done
gmonitoring opened this issue Jul 20, 2023 · 3 comments · Fixed by #54226
Closed
1 task done

Next-link props #52929

gmonitoring opened this issue Jul 20, 2023 · 3 comments · Fixed by #54226
Labels
bug Issue was opened via the bug report template. locked TypeScript Related to types with Next.js.

Comments

@gmonitoring
Copy link

gmonitoring commented Jul 20, 2023

Verify canary release

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

Provide environment information

Operating System:        
      Platform: win32        
      Arch: x64
      Version: Windows 10 Pro
    Binaries:
      Node: 18.7.0
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 13.4.7
      eslint-config-next: 13.4.8
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.3

warn  - Latest canary version not detected, detected: "13.4.7", newest: "13.4.11-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 or a replay of the bug

https://codesandbox.io/p/sandbox/runtime-resonance-rvkftd?file=%2Fsrc%2Fcomponents%2FBaseLink.tsx%3A10%2C22

codesandbox failed to reproduce.

To Reproduce

import React, { FC } from 'react'
import { Box } from '@/shared/ui'
import { Typography } from '@mui/material'
import ArrowRight from 'public/icons/arrow_right.svg'
import Link, { LinkProps } from 'next/link'


export type BaseLinkProps = {
  text: string
} & LinkProps

const BaseLink: FC<BaseLinkProps> = ({ text, ...props }) => {
  return (
    <Link {...props}>
      <Box
        flexDirection="row"
        justifyContent="space-between"
        alignItems="center"
        width="100%"
        mb={2}
      >
        <Typography variant="subtitle1">{text}</Typography>
        <ArrowRight />
      </Box>
    </Link>
  )
}

export default BaseLink

Describe the Bug

image

This error occurs when running tsc --pretty

Ide does not highlight this error without running the tsc --pretty command

Expected Behavior

I expected everything to be fine, because LinkProps is not Generic

Which browser are you using? (if relevant)

Chrome

How are you deploying your application? (if relevant)

next-dev
localhost

@gmonitoring gmonitoring added the bug Issue was opened via the bug report template. label Jul 20, 2023
@github-actions github-actions bot added the Navigation Related to Next.js linking (e.g., <Link>) and navigation. label Jul 20, 2023
@shuding
Copy link
Member

shuding commented Jul 20, 2023

If you have typedRoutes enabled, you can try the following:

CleanShot 2023-07-20 at 12 28 17@2x

It's unfortunate that I can't think of a way in TS to have generics in : FC<..>, only came with the workaround above.

@shuding shuding added TypeScript Related to types with Next.js. and removed Navigation Related to Next.js linking (e.g., <Link>) and navigation. labels Jul 20, 2023
@gmonitoring
Copy link
Author

gmonitoring commented Jul 20, 2023

image

This is the tenth time I've edited the answer. In general, my typescript was buggy initially, but in the end your solution saved me. THANK YOU

@kodiakhq kodiakhq bot closed this as completed in #54226 Aug 18, 2023
kodiakhq bot pushed a commit that referenced this issue Aug 18, 2023
Closes #53732. Closes #52929.

When using the statically typed routes feature, we might have code like:

```ts
export function Card<T extends string>({ href }: { href: Route<T> | URL })...
export function Card<T extends string>({ href }: LinkProps<T>)...
```

To statically check `<Card href={...}>` and make sure it's `href` is an existing route. However, in certain cases these route types are not generated (e.g. running `tsc` directly w/o a `next dev` or `next build`), which results in TS errors.

This PR adds stub generics to `Route` and `LinkProps` so even if that plugin isn't executed, these types will not block type checking.
@github-actions
Copy link
Contributor

github-actions bot commented Sep 2, 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 2, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 2, 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 TypeScript Related to types with Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants