Skip to content

Conversation

@kiner-tang
Copy link

No description provided.

@zpao
Copy link
Owner

zpao commented Aug 17, 2023

Can you explain what goal you'd like to achieve with this?

@kiner-tang
Copy link
Author

Currently, using internal types is cumbersome and requires complex type calculations, so we want to export them directly from the library.

@zpao
Copy link
Owner

zpao commented Aug 18, 2023

Are you writing a component that wraps and forwards props? That's fair but not clear if that's your intent. I'm not opposed to exporting types but want to be intentional about additional API surface area.

@etienne-beylier
Copy link

etienne-beylier commented Mar 28, 2025

Are you writing a component that wraps and forwards props? That's fair but not clear if that's your intent. I'm not opposed to exporting types but want to be intentional about additional API surface area.

Hi there, I was in this scenario it would be nice to have access to types?

@zpao
Copy link
Owner

zpao commented Mar 28, 2025

There are several variations of what people might need and there are utility types which get there, so I'm inclined to just say no here. Also, as would be written, these types are not the whole truth. They don't include the ref or the DOM properties that are supported. In fact I removed DOM specific style from the types in #357 because we get it for free with forwardRef.

For example, to get the full type of props:

import {QRCodeSVG} from 'qrcode.react';
import type {ComponentProps} from 'react';

type QRCodeSVGProps = ComponentProps<typeof QRCodeSVG>

Admittedly, this may not be precisely what you but it will be accurate. You can go a step further if you need just the explicit props, which I did in the tests. As you can see from the comment though, that's really just the make TypeScript happy when creating a bunch of props objects outside normal usage:

import type {ComponentPropsWithoutRef} from 'react';
// We're only using the explicit (non-DOM, non-ref) props for testing, but TS
// doesn't love us passing our test data as props without actually typing the
// object. So we'll make sure we give types that work. It's a little messy but
// gets the job done without being too fancy.
// Specifically, get the DOM-specific keys and omit them from the also-de-ref'ed
// component props. Then union together the two types since they may not always
// be the same.
type QRCodeCanvasProps = Omit<
ComponentPropsWithoutRef<typeof QRCodeCanvas>,
keyof React.CanvasHTMLAttributes<HTMLCanvasElement>
>;
type QRCodeSVGProps = Omit<
ComponentPropsWithoutRef<typeof QRCodeSVG>,
keyof React.SVGProps<SVGSVGElement>
>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants