microsoft / microsoft/TypeScript

Allow JSX element tags to be symbols

オープン
#38,367 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

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:

  1. Allow intrinsic elements to be upper-cased. This allows people to provide renderer-provided element APIs which mirror XML languages which are case-sensitive.
  2. Not globally scoped, so there is no possibility of namespace clashes, and can be imported/exported.
  3. Can still work with the global IntrinsicElements interfaces 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.

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

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

はじめの一歩

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

調査の方向性

この issue のエントリポイントは、JSX 要素の型チェックと JSX namespace の IntrinsicElements 宣言です。まず、既存の文字列、関数、クラスのケースがどのように検証されるかを追跡してください。Fragment と Portal の例を現在の TS2604 診断と比較し、symbol タグの JSX と symbol の IntrinsicElements キーのテストを定義してください。完了条件は、有効な symbol タグが、出力される JavaScript を変更せずに受け入れられることです。

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

評価

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

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

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