microsoft / microsoft/TypeScript

Hide prototype completions for properties of a component in JSX context

Abierto
#59,486 2 comentarios 5 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Experience Enhancement Help Wanted Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

🔍 Search Terms

prototype methods, hide suggestions, Function.prototype, callable type without prototype, compound components, JSX subcomponents

✅ Viability Checklist
⭐ Suggestion

Some UI components in libraries like React or Solid can expose "subcomponents" as properties. This pattern is commonly known as "compound components", which can be implemented in multiple ways. In this variant, there is a function ("root component") with properties that are functions ("subcomponents").

When typing JSX, TypeScript's autocomplete helpfully displays available subcomponents. However, Function.prototype properties and methods also show up.

This isn't a great experience, since those can never be used in this context - Function.prototype properties are never valid components in any UI framework/library.

For this reason, I'm suggesting that TypeScript never displays prototype methods when in this context (JSX opening element position).


More context:

In my way to finding a solution to this DX issue, my initial aim was trying to manually create some sort of "callable type" for the root component that somehow excludes prototype methods. I discussed with @Andarist on Twitter and my teammate @ciampo found this related issue ("Allow to add a call signature to the Mapped Type OR to remove all Function.prototype methods") that asks for a similar approach.

However, @Andarist suggested the solution above. I think this approach is superior for a variety of reasons:

  • Zero changes required by users. Works out of the box.
  • Very narrow scope.
  • Has absolutely no potential downsides AFAIK (as state, Function.prototype properties are never valid components in any UI framework/library)
  • I'm not very familiar with the TypeScript codebase - that said it feels like this would be a much simpler and straightforward change to implement.
📃 Motivating Example

Here's a compound component in React (19 for simplicity, since it doesn't require forwardRef) in what I call "overloaded" style (root function component with function properties as subcomponents):

function Component () {}
Component.Subcomponent = function Subcomponent () {}

When using it, the user will input something like this into their editor (playground link):

<Component>
  <Component.
            ^ TypeScript will display suggestions at this point

The suggestions will appear:

image

As you can see, prototype methods show up. Ideally, they wouldn't.

💻 Use Cases

Authoring components in the "overloaded" compound component API style, with better DX for consumers of the components.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Reproduce el ejemplo motivador de JSX compound-component e inspecciona el comportamiento del autocompletado en la posición Component. de un elemento JSX de apertura. Determina cómo se incluyen los miembros del prototipo en estas sugerencias y añade cobertura que demuestre que los subcomponentes válidos siguen disponibles, mientras que los miembros de Function.prototype no lo están.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
react, typescript
Área
developer-experience, frontend
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.