-
-
Notifications
You must be signed in to change notification settings - Fork 938
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
Declare web component in solid typescript #616
Comments
I think the problem is that works on the global namespace and it needs to work on Solid's. I've done it the way posted above in the past but that was back when we were too polluting the global namespace like React. So perhaps: declare module "solid-js" {
namespace JSX {
interface IntrinsicElements {
"component1-component": any
}
}
} |
Honestly, TypeScript differentiating between this confuses me. Anyone with TS experience with custom elements can chime in. @trusktr maybe. |
You should be able to perform this module augmentation in a declaration file (.d.ts) which will make it available everywhere in your app. The trick is to also import 'solid-js' in that declaration file. Without the import, the new module declaration clobbers the existing one instead of extending it. Here is an example: https://codesandbox.io/s/solid-counter-forked-ulhhj?file=/my-web-components.d.ts Please also note codesandbox falls down sometimes dealing with this stuff - reloading usually fixes it. |
works perfectly, thanks!. |
Chiming in with a potential improvement on the ideas/code snippets in this thread: There was a discussion/thread in a WC library called Shoelace on how to use the components in Solid. As part of that discussion, @justinfagnani (a member of the Lit team at Google) offered the below declaration file snippet as a way to get even better type annotations on web components used in Solid.
^(Link to Justin's comment for more context) It's also worth noting, however, that this will only work for web component libraries that add their components to the global Hope it helps anyone still passing by on this thread! |
If you extend declare module "solid-js" {
namespace JSX {
type ElementProps<T> = {
[K in keyof T]: T[K] extends Component<infer P> ? P : never
}
interface IntrinsicElements extends ElementProps<HTMLElementTagNameMap> {}
}
} Unfortunately this code does not error if you pass props to a component that has no props, like The default generic on Component is type ElementProps<T> = {
[K in keyof T]: T[K] extends Component<infer P>
? P extends Record<string, never>
? boolean // this is never hit
: P
: never // though this is hit, which correctly yields errors if, say, you pass a string to an number prop
} For now I think I'll just have two |
extract it from index.tsx allow for extension by importing solid-js itself to not overwrite but extend and make HTMLAttributes known see: solidjs/solid#616 (comment) and https://github.com/solidjs/solid/blob/89baf1206a1df27ab5a666b6c31729fd1505e341/packages/solid/h/jsx-runtime/src/jsx.d.ts
@ryansolid Any plans or information to support this in solidjs? Or at least a documentation note, that using web components with typescript requires this? |
There was a documentation note at one point about what I posted above. But, looking now it doesn't seem to have made it to the new Docs. So I think there could be a note about how to declare web components in the TS section. Opened an issue: solidjs/solid-docs#823 |
have been tried implement web component in solid typescript but i dont really know how to declare the component name, it show an error like this.
i have tried to declare it on file declaration.d.ts like this.
is there any way to declare web component name in solid typescript ?, thanks!.
The text was updated successfully, but these errors were encountered: