lit / lit-element

LEGACY REPO. This repository is for maintenance of the legacy LitElement library. The LitElement base class is now part of the Lit library, which is developed in the lit monorepo.
https://lit-element.polymer-project.org
BSD 3-Clause "New" or "Revised" License
4.49k stars 319 forks source link

Using with React + Typescript #1172

Closed makoven closed 2 years ago

makoven commented 3 years ago

Hello. Is it possible to somehow get the types for the React JSX (TSX). Every time I try to use an lit-element inside a JSX component, I get the error:

Property 't-btn' does not exist on type 'JSX.IntrinsicElements'

https://codesandbox.io/s/react-typescript-forked-uohq1?file=/src/index.tsx (example from https://github.com/Polymer/lit-element/issues/1052)

justinfagnani commented 3 years ago

I'm not sure what's causing JSX intrinsics checking to kick in - I thought intrinsics should only be checked if IntrinsicElements is non-empty - but since it is, you'll need to define an interface for <t-btn>:

interface TButton {
  count: number;
}

declare global {
  interface HTMLElementTagNameMap {
    "t-btn": MyElement;
  }
  /* eslint-disable @typescript-eslint/no-namespace */
  namespace JSX {
    interface IntrinsicElements {
      "t-btn": TButton;
    }
  }
}

https://codesandbox.io/s/react-typescript-forked-7lwx3?file=/src/t-btn.ts

makoven commented 3 years ago

@justinfagnani

Of course, I can manually define the desired interface. It is simply not clear why the library does not have a helper for inferring the desired interface from the lit-element component class. As a result, all the lit-element components that I tried cannot be used in the JSX frameworks

kevinpschaaf commented 2 years ago

You could consider using https://github.com/lit/lit/tree/main/packages/labs/react to create a React wrapper which you can use in TSX with good typings.

taylor-vann commented 2 years ago

Related to #2648

Closing :) webcomponent types are reflected in React props with the the react wrapper @kevinpschaaf referenced. Event handler types can be typecasted as well.

RayLuxembourg commented 1 year ago

Related to #2648

Closing :) webcomponent types are reflected in React props with the the react wrapper @kevinpschaaf referenced. Event handler types can be typecasted as well.

That's not true, when you want to have a required prop, it is not reflected at all, everything is options Is there a solution for this problem, I am unable to reflect types properly...

Screen Shot 2023-01-04 at 20 14 15 Name should be required not optional

justinfagnani commented 1 year ago

@RayLuxembourg for custom elements, all properties are necessarily optional, because you can always do document.createElement('my-element') and then provide no additional property assignments.

If we have a feature request on the React wrapper for transforming some properties, we should open that on the lit/lit monorepo for the @lit-labs/react package.