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

Button component as={Link} triggers typecheck error #1002

Closed
2 tasks done
shixiao opened this issue Sep 26, 2023 · 5 comments · Fixed by #1244 or #1325
Closed
2 tasks done

Button component as={Link} triggers typecheck error #1002

shixiao opened this issue Sep 26, 2023 · 5 comments · Fixed by #1244 or #1325
Labels
🐛 bug Something isn't working confirmed This bug was confirmed

Comments

@shixiao
Copy link

shixiao commented Sep 26, 2023

  • I have searched the Issues to see if this bug has already been reported
  • I have tested the latest version

Steps to reproduce

  1. Go to 'this sandbox'
  2. Run task dev (which does yarn dev)

Current behavior

The second button (<Button as={Link} href="www.example.com">Toggle 2</Button>) triggers a typechecker error:

Type '{ children: string; as: ForwardRefExoticComponent<Omit<AnchorHTMLAttributes<HTMLAnchorElement>, keyof InternalLinkProps> & InternalLinkProps & { ...; } & RefAttributes<...>>; href: string; }' is not assignable to type 'Pick<Omit<AnchorHTMLAttributes<HTMLAnchorElement>, keyof InternalLinkProps> & InternalLinkProps & { ...; } & RefAttributes<...>, "slot" | ... 263 more ... | keyof InternalLinkProps>'.
  Types of property 'as' are incompatible.
    Type 'ForwardRefExoticComponent<Omit<AnchorHTMLAttributes<HTMLAnchorElement>, keyof InternalLinkProps> & InternalLinkProps & { ...; } & RefAttributes<...>>' is not assignable to type 'Url | undefined'.typescript(2322)

Expected behavior

No typechecker error.

Context

#885 made a series of Button and ButtonProps types / interfaces generic, attempting to allow as={...}. For some reason, this is not working for as={Link}.

@shixiao
Copy link
Author

shixiao commented Sep 26, 2023

https://codesandbox.io/p/sandbox/dropdownas-f992mv?file=%2Fpages%2Findex.tsx%3A6%2C29

as={Link} and ref={ref} are also not working on Dropdown.Item. (forwardRef used to work on them)

cc: @nigellima @rluders

@rluders rluders added 🐛 bug Something isn't working confirmed This bug was confirmed labels Sep 27, 2023
@rluders
Copy link
Collaborator

rluders commented Sep 27, 2023

This impacts #1001

@mdtausifiqbal
Copy link

<Dropdown.Item as="a" href="https://flowbite.com/" target="_blank"> External link </Dropdown.Item>

This is not working for me and also as={Link} not working

@pangolingo
Copy link

I've avoided this in my codebase by creating a custom component that passes props to the button component:

import {
  Button as FlowbiteButton,
  ButtonProps as FlowbiteButtonProps,
} from "flowbite-react";
import Link from "next/link";

export default function Button(
  props: FlowbiteButtonProps<"a" | "button" | typeof Link>
) {
  return <FlowbiteButton theme={buttonTheme} {...props} />;
}

I can use the ButtonProps generic to tell Flowbite which types of "as" to expect.

@dima-kov
Copy link

any updates on this?

nigellima added a commit to gistia/flowbite-react that referenced this issue Jan 24, 2024
`as` prop was throwing TS errors when using Next Link component. Now this correctly infers the type
from any component passed to the `as` prop

fix themesberg#1002 fix themesberg#1107
SutuSebastian pushed a commit that referenced this issue Jan 24, 2024
…tton `as` prop (#1244)

* fix(button): correctly infer type from `as` prop

`as` prop was throwing TS errors when using Next Link component. Now this correctly infers the type
from any component passed to the `as` prop

fix #1002 fix #1107

* fix(dropdown-item): applied the generic types to DropdownItem and also wraps it with a forwardRef

* docs(button): removed now-unnecessary @ts-expect-error comments

* fix(button): replaced use of old genericForwardRef
abdulbasithqb pushed a commit to abdulbasithqb/flowbite-react that referenced this issue Jan 31, 2024
…tton `as` prop (themesberg#1244)

* fix(button): correctly infer type from `as` prop

`as` prop was throwing TS errors when using Next Link component. Now this correctly infers the type
from any component passed to the `as` prop

fix themesberg#1002 fix themesberg#1107

* fix(dropdown-item): applied the generic types to DropdownItem and also wraps it with a forwardRef

* docs(button): removed now-unnecessary @ts-expect-error comments

* fix(button): replaced use of old genericForwardRef
abdulbasithqb pushed a commit to abdulbasithqb/flowbite-react that referenced this issue Jan 31, 2024
…tton `as` prop (themesberg#1244)

* fix(button): correctly infer type from `as` prop

`as` prop was throwing TS errors when using Next Link component. Now this correctly infers the type
from any component passed to the `as` prop

fix themesberg#1002 fix themesberg#1107

* fix(dropdown-item): applied the generic types to DropdownItem and also wraps it with a forwardRef

* docs(button): removed now-unnecessary @ts-expect-error comments

* fix(button): replaced use of old genericForwardRef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working confirmed This bug was confirmed
Projects
None yet
5 participants