microsoft / microsoft/TypeScript
Hide prototype completions for properties of a component in JSX context
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.4k
- Merge trung bình
- 1 ngày 19 giờ
- Pull request đã merge (30 ngày)
- 117
Mô tả
🔍 Search Terms
prototype methods, hide suggestions, Function.prototype, callable type without prototype, compound components, JSX subcomponents
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ 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.prototypeproperties 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:
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.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Tái hiện ví dụ JSX compound-component làm cơ sở và kiểm tra hành vi hoàn thành mã tại vị trí Component. trong một phần tử mở JSX. Xác định cách các thành viên của prototype được đưa vào những gợi ý này, sau đó bổ sung kiểm thử cho thấy các subcomponent hợp lệ vẫn khả dụng, trong khi các thành viên của Function.prototype thì không.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- react, typescript
- Lĩnh vực
- developer-experience, frontend
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100