nextui-org / tailwind-variants

🦄 Tailwindcss first-class variant API
https://tailwind-variants.org
MIT License
2.42k stars 68 forks source link

Incorrect types inheritance when extending #44

Closed vanpav closed 11 months ago

vanpav commented 1 year ago

Describe the bug When using "extend" prop to compose class names, the prop-types produced in extended TVReturnType aren't correct. Without "boolean" variant, typescript happy, but the type of "props.variant" in the BuyButton component is "string | number | undefined" which looks incorrect too.

To Reproduce

const baseButton = tv({
  base: "",
  variants: {
    variant: {
      primary: "",
    },
    disabled: {
      true: "",
    },
  },
});

const buyButton = tv({
  extend: baseButton,
});

const Button = (props: VariantProps<typeof baseButton>) => {
  const className = baseButton(props);

  return (
    <div className="flex gap-3">
      <button className={className}>Buy button</button>
    </div>
  );
};

const BuyButton = (props: VariantProps<typeof buyButton>) => {
  const className = buyButton(props);

  return (
    <div className="flex gap-3">
      <button className={className}>Buy button</button>
    </div>
  );
};

Expected behavior Props for the "buyButton" inherited properly to let typescript pass without errors and suggest correct variants in autocomplete.

Screenshots

Screenshot 2023-05-03 at 17 58 15 Screenshot 2023-05-03 at 17 58 26

Desktop (please complete the following information):

Additional context

eduardodallmann commented 1 year ago

I think I encountered a similar problem. Boolean variant breaks typescript only when it is extended. See the example:

Codesandbox Fork is required to see the typescript error