microsoft / microsoft/TypeScript

Hide prototype completions for properties of a component in JSX context

Offen
#59,486 2 Kommentare 5 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Experience Enhancement Help Wanted Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔍 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere das motivierende JSX-Compound-Component-Beispiel und untersuche das Vervollständigungsverhalten an der Component.-Position in einem öffnenden JSX-Element. Ermittle, wie Prototypmitglieder in diese Vorschläge aufgenommen werden, und füge anschließend eine Abdeckung hinzu, die zeigt, dass gültige Subkomponenten weiterhin verfügbar sind, während Mitglieder von Function.prototype nicht verfügbar sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
react, typescript
Bereich
developer-experience, frontend
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.