microsoft / microsoft/TypeScript
Allow JSX intrinsics to have generic call signatures
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
Search Terms
type checking, jsx intrinsics, generic type, signature
Suggestion
Allow JSX intrinsics defined in IntrinsicElements to support generic call signatures, as is supported for user-defined SFCs.
Use Cases
Suppose you want to create a small DSL using JSX. You would need to model the primitive statements of the DSL. Your options are intrinsics and user-level components.
Intrinsics are clearly the more natural fit for this use case. Some reasons are:
- they are immediately available without separate import statements,
- the use of lower case makes them visually distinct from user-defined components,
However, intrinsics are not as capable as user-level components (pre-defined or user-defined). Currently they cannot be assigned generic type signatures. This makes them less expressive, and so, less useful.
The proposed change will bring parity between intrinsics and user-level components. In short, it will be possible to create a pre-defined set of intrinsics that are just as expressive as user-level SFCs.
Examples
Consider the following hypothetical DSL in JSX:
<for items={books} with={book => book.genre === 'scifi'}>
{book => (
<b>{book.title}</b>
)}
</for>
Currently, the for intrinsic cannot be generic over the type of the items, unlike user-defined SFCs which may be generic. To bring this capability to intrinsics, we would need to allow JSX.IntrinsicElements to support generic signatures. Here is what it might look like for this example DSL:
declare namespace JSX {
interface IntrinsicElements {
for: <T>(props: {
items: T[],
with?: (item: T) => boolean,
children?: (item: T) => Element | Element[]
}) => Element;
b: { children: string }
}
}
The implementation of this change will require a small number of changes to src/compiler/checker.ts. A good starting point is the function resolveJsxOpeningLikeElement.
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.
Request to work on this feature
I would like to work on this feature improvement.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 src/compiler/checker.ts 中的 resolveJsxOpeningLikeElement 开始,比较 JSX intrinsic 和用户定义的 SFC 是如何解析的。将 issue 中的 IntrinsicElements 示例作为目标行为。完成的标准是接受 intrinsic 上的泛型调用签名,并使其类型参数贯穿 props 和 children。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 35/100