microsoft / microsoft/TypeScript
Allow JSX element tags to be symbols
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.4k
- 平均合并
- 1 天 19 小时
- 30 天内合并 PR
- 117
描述
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.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
该 issue 的入口点是 JSX 元素类型检查以及 JSX namespace 的 IntrinsicElements 声明;首先跟踪现有的字符串、函数和类用例是如何验证的。将 Fragment 和 Portal 示例与当前的 TS2604 诊断进行比较,并为 symbol 标签 JSX 和 symbol IntrinsicElements 键定义测试;完成标准是接受有效的 symbol 标签,同时不改变生成的 JavaScript。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 30/100