microsoft / microsoft/TypeScript

Stronger existence guarantees for `declare`

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

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

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

説明

🔍 Search Terms

"declare", "exist", "extern equivalent", "generate"

✅ Viability Checklist
⭐ Suggestion

Some sort of extern keyword, similar to declare but with stronger existence checks, intended to remove the need to generate "ephemeral" code (e.g., making build information available at runtime) at least once to get the editor to not complain while still maintaining type safety.

📃 Motivating Example

In, e.g., C/C++, one way to inject build information into a final build of a binary is simply to write a header to let the editor know about it and then generate a file containing the data at build time. This is safe, since in C/C++ the compiler will ensure that extern const char *version; is backed up by an actual const char *version = ""; somewhere else, otherwise the build will fail. This process can be more manual (e.g., handwrite the header, handwrite a script to generate the file) or somewhat automated (e.g., autoconf, CMake).

In TS, the equivalent of header files is .d.ts or declare statements. However, it is a promise that doesn't get checked, which, to my understanding, is to allow type checking of libraries not written in TS, without having to run the entire library through tsc, which might not be possible anyway (e.g., closed source minified scripts at a CDN).

Since the TS compiler (currently) doesn't perform that check, my options are:

  • Remove the .d.ts "header" and require the file to be generated before opening the project in an editor to avoid errors about importing a non-existent file. Probably the simplest way to go, but this sacrifices in ergonomics.
  • Find some way to let a .ts override a .d.ts file. Probably breaks some existing use case, thus not desirable.
  • Perform the check manually, e.g., in addition to running tsc, write a script that uses the TS Compiler API to parse .d.ts files and ensure it has a matching .ts file, and that the stuff the "header" export declares are actually what they say they are. This is the rabbit hole I've been going down.
  • Find some other way to express "I promise this thing will exist at build time, and thus its type can be checked later" in TS. Currently, .d.ts only expresses "I promise it will exist at runtime", but it has no way to perform a build time check.
💻 Use Cases
  1. What do you want to use this for? Generating ephemeral but important information (e.g., build information), or otherwise ensuring something exists when it is possible to check, i.e., it is expected to exist at build time, even if not all the time.
  2. What shortcomings exist with current approaches? The check must be peformed manually (complex plumbing to get the compiler to do something it already can do, but doesn't) or not at all (sacrificing in ergonomics, as it requires generation to be run, even if the results are instantly stale, just to satisfy the editor and type checker).
  3. What workarounds are you using in the meantime? Running the check manually.

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

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

はじめの一歩

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

調査の方向性

ソースファイルもテストも指定されていません。まず、TypeScript が現在 declare 文と .d.ts ファイルをどのように処理しているかを確認し、その挙動を記載されている Compiler API の回避策と比較してください。この作業には、出力される JavaScript を変更せずに、ビルド時の存在チェックをより厳密にするための、確定した設計が必要になります。

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

評価

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

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

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