microsoft / microsoft/TypeScript

API: add function to resolve global symbol

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

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

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

説明

Suggestion

🔍 Search Terms

symbol, constructor, well known, global

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Expose getGlobalSymbol on TypeChecker

📃 Motivating Example

TypeScript applies some special handling to global declarations, e.g. Array, Object, Function, ReadonlyArray,
The recent change to well known Symbols adds yet another special case for the global Symbol value symbol.

Currently there's no reliable way to obtain the symbol of a global declaration.

💻 Use Cases

To find out whether a type is iterable, I need to look for a __@iterable property. Previously that worked well with a hard coded string. Now I need to look up the global Symbol constructor type, get the type of its property iterator and use the escapedName for the lookup on the type in question. The first step is actually the tricky one, because there's no API for it.

There are a few workarounds I can think of:

  • the hacky way: checker.getSymbolsInScope('' as any, ts.SymbolFlags.Value).filter(s => s.escapedName === 'Symbol')
  • the not so obvious way:
    • find a property whose name starts with __@ since those are symbol names
    • its valueDeclaration must be a NamedDeclaration with a ComputedPropertyName
    • get the type of the computed property name to obtain a reference to the (unique) symbol type
    • getApparentType to obtain a reference to the global Symbol type
    • its symbol should hopefully always refer to the global Symbol constructor

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

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

はじめの一歩

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

調査の方向性

TypeChecker API と、issue で説明されている既存のグローバル宣言の特殊ケース(Array、Object、Function、ReadonlyArray、Symbol を含む)から始めます。公開の getGlobalSymbol エントリポイントが checker API にどのように適合するかを判断し、グローバル宣言の解決に関するカバレッジを追加します。完了の条件は、コンシューマーが列挙された workaround なしでグローバルな Symbol コンストラクターのシンボルを確実に取得できることです。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

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

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