microsoft / microsoft/TypeScript
Add getMappedTypeParameters to TypeChecker
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
Search Terms
TypeMapper, mapper, mapper1, mapper2
Suggestion
To help transformer developers: Add a helper on TypeChecker to provide (and expose) the inferred (mapped) types of a source function call. i.e.
given the source:
function foo<C>(ctor: C)...
foo(Bar);
provide transformer developers a method (on ts.TypeChecker)
getMappedTypeParameters(node: ts.CallExpression): ts.Type[]
so they can get the inferred TypeParameter nodes for a given source call-expression. By adding a helper method, it does not expose the internal mapping, limits the required maintenance if the internal mapping is ever changed, yet allows developers access to the inference information.
Use Cases
During AST code transform, the compiler has already inferred the types passed to source functions, without them being explicitly defined as a type parameter in the call. The current Signature (typeChecker.getResolvedSignature(node)) does not expose these inferred types and the ```signature.typeParameters`` remains undefined
For transformers that require this information, the current work around is use the internal "mapper" field on Signature. This mapper field changed in ts 3.9 and potentially could change again. Exposing an ability to access this mapping via the TypeChecker should help reduce breakage and limit maintainance required by the TypeScript Team to expose and execute the mapping.
Examples
In my DI transformer, I attempt to infer Type parameters and use them to manipulate the compiled output to configure the DI Container. For example; the source code:
function main() {
let kernel = new Kernel();
kernel.bind(toSelf(Bar));
kernel.bind(toSelf(Foo));
}```
is transpiled to:
```js
function main() {
let kernel = new pigly_1.Kernel();
kernel.bind(Symbol.for("Bar"), pigly_1.toSelf(Bar));
kernel.bind(Symbol.for("Foo"), pigly_1.toSelf(Foo, ctx => ctx.resolve({ service: Symbol.for("Bar"), name: "bar" }).first()));
}
As relevant to this proposal, my transpiler needs to be able to infer the TypeParameter of .bind<T> so that it can emit a symbol for it into the runtime code.
My Current work around to discover the inferred mappings is here:
https://github.com/pigly-di/pigly/blob/develop/packages/pigly-transformer/src/index.ts#L284-L331
Admittedly this is a very niche proposal (if allowed I'd happily do a PR to add it and do the tests) - but at least one other developer had trouble with this: https://stackoverflow.com/questions/48886508/typechecker-api-how-do-i-find-inferred-type-arguments-to-a-function
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.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず TypeChecker API と issue で説明されている getResolvedSignature エントリポイントから始め、Signature.mapper が現在どのように推論されたマッピングを提供しているかを調べます。要求されている getMappedTypeParameters の動作を、既存の内部 mapper と transformer による workaround と比較します。TypeChecker が内部の Signature フィールドを必要とせずに呼び出し式の推論された型パラメーターを公開し、ここで説明されている generic-call の例をカバーできれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 30/100