microsoft / microsoft/TypeScript

Hide prototype completions for properties of a component in JSX context

オープン
#59,486 コメント 2 件 リアクション 5 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Experience Enhancement Help Wanted Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

動機となった JSX compound-component の例を再現し、JSX の開始タグ内の Component. の位置での補完動作を調べます。これらの候補にプロトタイプのメンバーがどのように含まれるかを確認し、有効なサブコンポーネントは引き続き利用できる一方で、Function.prototype のメンバーは利用できないことを示すカバレッジを追加します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
react, typescript
領域
developer-experience, frontend
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。