microsoft / microsoft/TypeScript
type inference issue with dynamic template literals
オープン
まだ誰も着手していません。
Domain: check: Type Inference
Help Wanted
Possible Improvement
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 Search Terms
"template literals and type inference", "type inference with dynamic template literals"
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about it
⏯ Playground Link
💻 Code
type Api = {
[key: `hello.${string}`]: (data: { name: string }) => void
}
let userId: string = 'dynamic string'
// Example1: Doesn't work. type for `e` can't be inferred
const test1: Api = {
[`hello.${userId}`]: e => {
console.log(e.name)
},
}
// Example2: Works
const test2: Api = {
[`hello.static_string`]: e => {
console.log(e.name)
},
}
// Example3: Works
const on = <K extends keyof Api>(
key: K,
action: (data: Api[K]) => void,
) => {
// some logic
}
on(`hello.${userId}`, e => {
console.log(e.name)
})
🙁 Actual behavior
Example 1 doesn't work, while Example 2 and Example 3 works properly. The type for e can't be inferred
🙂 Expected behavior
To infer type of e in the Example 1.
Additional information about the issue
No response
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた TypeScript Playground から始め、3 つの例の違いを再現します。特に、Example 1 における動的な template-literal プロパティの文脈的型付けを確認してください。e の推論された型を、静的キーの場合およびジェネリック関数の場合と比較します。Example 1 で e が示されている { name: string } パラメーター型として推論されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 42/100