microsoft / microsoft/TypeScript

Symbol definition improvement

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

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

In Discussion Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Search Terms

symbol in:title, symbol improvement, symbol description definition

Suggestion

By using more and more symbol in my projects, and to make debugging easier in development mode, I fill the optional description to Symbol. Then, on production, I remove all description during the bundling process:
image

So I was thinking that it could be really useful to have the same approach for TypeScript - playground:
image
image

Just imagine we don't have xsymbol<"test"> but the "native" symbol<"test"> or symbol("test") to be closer to DevTools.

Then, by extending this approach, I don't know if it could be helpful to increase the typing inference.
Given this code:

const foo = {
    type: 'foo' as 'foo',
    foo: ''
};

const bar = {
    type: 'bar' as 'bar',
    bar: ''
};

type FooBarAction = typeof foo | typeof bar;

function getType<T>(input: {type: T}): T {
    return input.type;
}

function foobar(action: FooBarAction) {
    switch (action.type) {
        case getType(foo):
            action.foo;
            break;
        case getType(bar):
            action.bar;
            break;
    }
}

image

Here, according to the type property value, the action provide correctly foo property or bar property.

But, if I replace them by symbol:

const sFoo = {
    type: Symbol('sfoo'),
    sfoo: ''
};

const sBar = {
    type: Symbol('sbar'),
    sbar: ''
};

type SFooBarAction = typeof sFoo | typeof sBar;

function sfoobar(action: SFooBarAction) {
    switch (action.type) {
        case getType(sFoo):
            action.sfoo;
            break;
        case getType(sBar):
            action.sbar;
            break;
    }
}

image
image

Here TypeScript cannot resolve properly the correct definition of my action variable and keep saying that it get the SFooBarAction type, so only type property is available.

According to this PR #30196, I understand that working on symbol in the core system of TypeScript is not an easy thing, but probably it could be helpful if the optional description of Symbol as a definition descriptor to have the unique symbol in the definition. But definitely, as soon as we provide same description, we will face the same problem as today.

Full Playground

Use Cases

For the first part, everywhere when we use Symbol.

For the second part, as soon as unique value is required, like for Event Emitter, Action in Redux architecture, ...

Examples

See above on the Suggestion section

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 番号を参照したプルリクエストを送ります。

調査の方向性

リンクされた TypeScript Playground の例を再現することから始め、string-discriminant cases と Symbol cases を比較し、関連する PR #30196 を確認してください。この作業には、Symbol descriptions、unique values、control-flow narrowing について確定した設計と、それに対応するコンパイラーの動作、および要求された推論を示すテストが必要になります。

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

評価

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

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

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