Skip to content
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

Support Preact Typescript #868

Closed
tigrr opened this issue Aug 23, 2022 · 7 comments · Fixed by #1565
Closed

Support Preact Typescript #868

tigrr opened this issue Aug 23, 2022 · 7 comments · Fixed by #1565
Labels
feature Feature requests. help wanted Ready for a contributor to tackle. upstream An upstream issue is blocking the issue.

Comments

@tigrr
Copy link

tigrr commented Aug 23, 2022

What issue are you having?

It looks like right now Shoelace's React version's type definitions don't work with projects in Preact. The components themselves work, but the props' types information is missing.

I created a demo project on GitHub. It is created using Vite preact-ts template. Please clone it and try to open the src/app.tsx file in your code editor or try running it through tsc.

On the main branch, if you add a not defined prop nonExistant="nope" on the SlButton (line 21), Typescript won't show it as an error, whereas adding this prop to a div above of course produces an error:

Type '{ children: Element[]; class: string; nonExistant: string; }' is not assignable to type 'HTMLAttributes<HTMLDivElement>'.
  Property 'nonExistant' does not exist on type 'HTMLAttributes<HTMLDivElement>'.ts(2322)

I tried installing @types/react@18.0.17 and @types/react-dom@18.0.6 on the react-types branch. It did solve the problem with the component types, but introduced new errors, such as:

(alias) const SlButton: React.ForwardRefExoticComponent<Partial<Omit<React.HTMLAttributes<Component>, "onSlBlur" | "onSlFocus"> & Omit<Component, "children" | "onSlBlur" | "onSlFocus" | "defaultChecked" | ... 251 more ... | "onTransitionEndCapture"> & {
    ...;
}> & React.RefAttributes<...>>
import SlButton
This JSX tag's 'children' prop expects a single child of type 'ReactNode', but multiple children were provided.ts(2746)

Describe the solution you'd like

I don't really know. Maybe others have some ideas how to implement support for Typescript and Preact in Shoelace or can offer workarounds. Other libraries (such as "material-ui" for example) work fine with Typescript Preact. Maybe this is something about @lit-labs/react.

Describe alternatives you've considered

The workaround I was trying was to install @types/react and @types/react-dom, but that doesn't work either.

@tigrr tigrr added the feature Feature requests. label Aug 23, 2022
@claviska claviska added the help wanted Ready for a contributor to tackle. label Aug 23, 2022
@claviska
Copy link
Member

I'd love to support this, but I'm also not sure what the right solution is. I'll dive in when I get some time, unless somebody has a suggestion here.

@claviska
Copy link
Member

See this tweet from @ParamagicDev:

Preact supports custom-elements out of the box, is there any reason to use the React wrappers if you can use the regular HTML elements?

He raises a good point. Are you mostly trying to use the Shoelace wrappers idiomatically? If so, did you come across any problems using the custom elements directly?

@cyantree
Copy link
Contributor

cyantree commented Mar 5, 2023

I'm playing around with the same thing. I'm experimenting with combining Preact elements and web components.

Using Shoelace componnents without a wrapper works, however the tags are unknown to JSX/TSX. Therefore the following error will be thrown:

Property 'sl-button' does not exist on type 'JSX.IntrinsicElements'.ts(2339)

So to solve this it seems to be necessary to provide the appropriate type declarations like this:

declare global {
    namespace preact.createElement.JSX {
        interface IntrinsicElements {
            'sl-button': {
                children: any;
                size?: 'small' | 'medium' | 'large' | undefined;
            };
        }
    }
}

Maybe it's somehow possible to get rid of preact.createElement too.

@cyantree
Copy link
Contributor

cyantree commented Mar 5, 2023

Here's another quite slim (and dirty?) wrapper to get it working with Preact until there's an official solution:

import type {SlButton as SlButtonNative} from '@shoelace-style/shoelace';
import '@shoelace-style/shoelace/dist/components/button/button.js';

export const SlButton = 'sl-button' as any as (props: {
    children?: any;
    'onsl-blur'?: ((e: CustomEvent) => void) | undefined;
} & Partial<Omit<SlButtonNative, 'children'>>) => any;

@claviska
Copy link
Member

claviska commented Mar 6, 2023

This is a bit of a stretch, and I don't expect a reply from Jason, but pinging @developit to see if there's a recommended solution for making types for React-wrapped custom elements work with Preact as well.

@claviska claviska removed their assignment Apr 4, 2023
@KonnorRogers
Copy link
Collaborator

Oh wait I have a gist for this, it definitely doesn't work as-is, but should get fairly close to what is needed to support Preact. It uses the regular HTML elements and not the React Wrappers.

https://gist.github.com/KonnorRogers/3dbbd28a41190f9ef8961f25775e814a

@augustjk
Copy link

augustjk commented Sep 1, 2023

Recent versions of preact include almost all compatible types that can replace those from react.

See this issue reported in the lit repo: lit/lit#4138 (comment)

This preactjs/preact#4124 is the missing piece to get proper type checking working. We might just copy that over to @lit-labs/react if preact/compat aren't open to adding it.

I tried to tweak the returned type from @lit-labs/react directly usable in preact without the compat mapping and it's almost there but breaks with typescript >= 5.1. It seems like less headache to go with the mapping.

To directly use custom elements in preact, users would need to add something like this:

import SlButton from '@shoelace-style/shoelace/dist/components/button/button.js';
import {
  SlBlurEvent,
  SlFocusEvent,
  SlInvalidEvent,
} from '@shoelace-style/shoelace';
import { LitElement } from 'lit';

declare module 'preact' {
  namespace JSX {
    interface IntrinsicElements {
      'sl-button': HTMLAttributes<SlButton> &
        Partial<Omit<SlButton, keyof LitElement>> & {
          'onsl-blur'?: (e: SlBlurEvent) => void;
          'onsl-focus'?: (e: SlFocusEvent) => void;
          'onsl-invalid'?: (e: SlInvalidEvent) => void;
        };
    }
  }
}

Partial<Omit<SlButton, keyof LitElement>> this could be better. Maybe picking from SlButton is better than omitting LitElement as some methods like render() can also show up in the suggestion. This is true of @lit-labs/react wrapped component as well though.

I don't know if there's a source for the event names the component fires that TS can pick up on without parsing a manifest to be able to auto generate this all within typescript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Feature requests. help wanted Ready for a contributor to tackle. upstream An upstream issue is blocking the issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants