microsoft / microsoft/TypeScript
Allow JSX element tags to be symbols
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.3k
- Merge trung bình
- 2 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 132
Mô tả
Search Terms
es6 symbols JSX createElement element tag type intrinsic host
Suggestion
Allow JSX element tags to be ES6 symbols instead of just strings, functions or classes.
Use Cases
JSX-based libraries like React have adopted the convention of using special upper-cased symbols as the tag of the JSX expression (<Fragment />, <Suspense />) as a way to provide intrinsic elements which aren’t callable and whose behavior are defined by the library rather than the tag.
Currently, TypeScript only allows JSX tags to be strings, functions or classes as defined by the JSX interface, and will throw errors like the following if Symbols are used:
error TS2604: JSX element type 'Fragment' does not have any construct or call signatures.
Libraries are therefore forced to lie about the actual typings of Symbol tags as something other than a unique symbol. For instance, React defines Symbol tags as ExoticComponent and pretends that the symbols are callable (https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L358-L364). The framework I work on (Crank.js) just defines symbols meant to be used as tags as any but this then has further repercussions when I attempt to, for instance, use these symbol types in interfaces.
Symbols are useful because they:
- Allow intrinsic elements to be upper-cased. This allows people to provide renderer-provided element APIs which mirror XML languages which are case-sensitive.
- Not globally scoped, so there is no possibility of namespace clashes, and can be imported/exported.
- Can still work with the global
IntrinsicElementsinterfaces to be strongly typed.
Examples
const Fragment = Symbol.for("crank.Fragment");
type Fragment = typeof Fragment;
const Portal = Symbol.for("crank.Portal");
type Portal = typeof Portal;
// should not error
const el = (
<Fragment>{children}</Fragment>
);
declare namespace JSX {
interface IntrinsicElements {
foo: any;
[Portal]: {root: any};
}
}
Checklist
My suggestion meets these guidelines:
- 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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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
Các điểm vào của issue là kiểm tra kiểu phần tử JSX và các khai báo IntrinsicElements của namespace JSX; trước tiên hãy lần theo cách các trường hợp chuỗi, hàm và lớp hiện có được xác thực. So sánh các ví dụ Fragment và Portal với chẩn đoán TS2604 hiện tại và định nghĩa các bài kiểm thử cho JSX với thẻ symbol và các khóa symbol của IntrinsicElements; hoàn thành có nghĩa là các thẻ symbol hợp lệ được chấp nhận mà không thay đổi JavaScript được phát ra.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- javascript, typescript
- Lĩnh vực
- compilers
- 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
- 30/100