microsoft / microsoft/TypeScript
Narrowing string down to a module specifier
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
🔍 Search Terms
string, narrow, module, import, specifier
✅ Viability Checklist
- 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 isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
Being able to declare a string as resolvable (a module specifier), without having to use an actual import or typeof import(specifier).
📃 Motivating Example
The following shows what this issue is about, using a hypothetical Resolvable:
// Would be nice if one could resolve/dereference these anywhere, not only inside `import`.
// Get intellisense and type checker support for string and identifier here:
const foo: Resolvable = "foo";
const bar: Resolvable = "bar";
// and here:
const b: Record<string, Resolvable> = {
[foo],
baz: bar,
};
// Nuh-uh: "Hello World!" is not resolvable;
b[foo] = "Hello World!";
// and anywhere;
await import(feelingBazyToday ? b["baz"] : b[foo]);
The following shows a workaround, with clearly different DX and architecture.
const foo = "foo";
const bar = "bar";
// Is the following pattern beneficial or does it hurt?
// To me it looks like useless wrappers and repetition.
// It's also harder to describe the `typeof a`, although
// one may rely on inference in some situations.
const a: Record<string, ((...x: any[]) => GoodLuck)> = {
foo: () => import(foo),
baz: () => import(bar),
};
// May or may not be ok, but it's a different problem.
a[foo] = console.log;
await (feelingBazyToday ? a["baz"] : a[foo])();
💻 Use Cases
- What do you want to use this for?
- What shortcomings exist with current approaches?
- What workarounds are you using in the meantime?
-
Improving DX in IDEs without compromising on runtime code. In particular it would help prevent errors everywhere a (future) module specifier string is decoupled from the dynamic import.
-
Dynamic imports have runtime effects. It's currently not possible to dereference a specifier before use (anywhere it might appear).
-
Either: Writing
typeof import(foo)(and deleting it again), when one wants to know (or navigate to) whatfoowould resolve to. This partially solves the problem, by effectively validating the string, but that information is lost. Or: A different architecture similar to the workaround in the motivating example. Also note, that the described feature can't be implemented in TS user land with things like utility types or type discrimination; so it's not merely sugar for existing functionality. Every workaround will lack a key aspect of the described feature.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
ソースファイル、テスト、エントリポイントのいずれも指定されていません。まず提案とその動機となる例を確認し、次に、解決可能な module-specifier 型が、出力される JavaScript を変更せずにチェックとナビゲーションをどのようにサポートできるかを判断してください。完了条件には、設計の決定と、それに対応する type-checker および IDE の動作が含まれます。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 25/100