nasheomirro / react-polymorphed

A set of types to help easily create fast polymorphic components.
MIT License
24 stars 3 forks source link

JSDOC / Comments not preserved? #6

Closed Sliov closed 1 year ago

Sliov commented 1 year ago

Hey there,

Thank you for providing this library, it has been super helpful!

I just noticed that the JSDOC / Typescripts comments are dropped when using the PolyRefFunction type, here's a quick example:

With a simple React.forwardRef, JSDOC/comments stay intact:

import * as React from 'react';
import { cva, type VariantProps } from 'class-variance-authority';

export const styles = cva('', {
  variants: {
    /**
     * The intent, or color, of the button.
     * @default primary
     */
    intent: {
      primary: 'bg-black',
    },

    /**
     * The size of the button.
     * @default medium
     */
    size: {
      small: 'p-1',
      medium: 'p-2',
      large: 'p-3',
    },
  },

  defaultVariants: {
    intent: 'primary',
    size: 'medium',
  },
});

type ButtonVariants = VariantProps<typeof styles>;

export type ButtonProps = React.ComponentPropsWithoutRef<'button'> &
  ButtonVariants;

export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  ({ intent, size }, ref) => {
    return (
      <button className={styles({ intent, size })} ref={ref}>
        Click me!
      </button>
    );
  }
);

const Example = () => (
  <div>
    <Button intent={}></Button>
  </div>
);
Screenshot 2023-07-03 at 9 49 15 pm

But disappears when using the polyRef helper:

export const polyRef = React.forwardRef as PolyRefFunction;

export const Button = polyRef<'button', ButtonProps>(
  ({ intent, size }, ref) => {
    return (
      <button className={styles({ intent, size })} ref={ref}>
        Click me!
      </button>
    );
  }
);
Screenshot 2023-07-03 at 9 49 25 pm

Do you know how to fix it or guide through a potential fix? I'm pretty new to TypeScript but happy to dig a bit more to help if pointed in the right direction ✌️

Here's the full gist, just in case: https://gist.github.com/Sliov/9966a3fd3ad5c886e6a192a563839284

nasheomirro commented 1 year ago

Still figuring out how to fix this, I did find out what the problem is however, something about how the comments are lost when using union types (playground here).

Now the problem here is that I use a janky trick for correctly inferring props which involves using unions to fix (#2 ). The good news is that that trick isn't needed anymore cause somehow miraculously typescript (i think ts-server) was now able to infer props correctly, the bad news is that in some cases where heavier computations on types are needed it incorrectly infers props again and the trick is again needed to correct it.

I'm unsure whether or not to remove the trick cause I haven't really checked if the problem still persist on heavy computations (last time I saw the problem was like 5 months ago), so I'm a bit lost on whether to remove it now or not.

nasheomirro commented 1 year ago

Ehh, what's the worst that could happen? I'll remove the work-around for now and trust that #2 doesn't come back, already released the fix as 2.2.0 so the comments should come up correctly once you update.

Sliov commented 1 year ago

Thank you @nasheomirro , works like a charm now 👍