[labs/react] export component and prop types
See original GitHub issueWhich package is this a feature request for?
React (@lit-labs/react)
Description
An explicit helper type for devs to use in JSXIntrinicElements
would reduce type conflicts for react clients.
declare namespace JSX {
interface IntrinsicElements {
foo: HelperType<CustomElement>;
}
}
Currently we rely on Typescript to infer what is returned from createComponent
and although TS is fairly good, an inference is not as durable as an explicit typing.
Related to: https://github.com/lit/lit/pull/3163
Alternatives and Workarounds
There are a few caveats to keep in mind:
- our package returns a
ForwardedRef
as a component - web components not created through
createComponent
still need proper typing, they are not aForardRef
Two types should be exported:
- type returned from
createComponent
- a type any dev can use for
IntrinsicElements
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:17 (9 by maintainers)
Top Results From Across the Web
Export component prop types - tailwindlabs/headlessui - GitHub
I have been doing React.ComponentProps for exactly the same use case (I wrap all the components I import) and it worked very well...
Read more >How to Overload TypeScript React Component Interfaces by ...
This article explains how to create correct, intuitive APIs for React components in TypeScript by using prop values to overload React component interfaces....
Read more >Typechecking With PropTypes - React
PropTypes exports a range of validators that can be used to make sure the data you receive is valid. In this example, we're...
Read more >How to Export Instance Methods From React Function ...
How to Export Instance Methods from React Function Components · First we use forwardRef · Defining our method object · Accessing the ref...
Read more >@lit-labs/react - npm
While React can render Web Components, it cannot easily pass React props to custom element properties or event listeners. This package provides ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
that’s pretty true. my main use case i guess is with multi-word attributes like
display-name
. say in my WC, i have a reactive propertydisplayName
with an attribute name defined from the Lit decorator asdisplay-name
.In JSX when using a WC tag, say I type the tag name and start typing some attribute
<x-foo d
. The intellisense would show medisplayName:string;
right? if I choose that from the context menu, then i’d have<x-foo displayName=“”
which wouldn’t be right? In JSX for WCs youve gotta use the actual attribute name, right?So if the typing isn’t smart enough to know that in JSX
displayName
should be autocompleted asdisplay-name
, then won’t devs who choose to use the autocomplete have to go back and change the autocomplete back todisplay-name=
?you’re right that the attribute typing would all be strings and booleans, but that’s all JSX can set on a WC tag right now anyway, right? maybe once they land support for setting either/both attrs and props on a WC it’ll get better?
So i tested the
WebComponentProps
type here and I found one error, and had one question.The error is that
class
isn’t accounted for. When using a web component as a web component in a react app, you can’t useclassName
, onlyclass
will work, right? So that needs to be reflected in the typing, and I don’t see it currently.The question is “Why can’t the typing be attribute-based instead of property-based?” Right now, when we’re seeing typing messages and autocomplete help bubbles in VSCode, they always contain the JS property names from my WC classes, but not the attribute names. But in react/JSX, only the attribute names work. So something like:
would show a typing hint like
alignX
oralignY
instead ofalign-x
andalign-y
. If the dev uses the autocomplete chooser, they’ll get:and that wont work. A related note is that the type above didnt seem to provide any attribute value hinting when you get the correct attribute. If you type:
you dont get any completion help for the values for alignY (‘start’, ‘center’, ‘end’ etc). Presumably thats because the JS prop and the HTML attribute have different names. IMO tho, the typing should reflect that somehow? Does that have to do with what is in HTMLElementTagMap?