microsoft / microsoft/TypeScript
TypeScript can't infer type of default parameters
オープン
まだ誰も着手していません。
Domain: check: Type Inference
Help Wanted
Possible Improvement
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 Search Terms
infer default parameters
function wrapper
function factory
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ
- I tried down to version 4.0.3 (since in the earlier version
Generatortype is not generic)
- I tried down to version 4.0.3 (since in the earlier version
⏯ Playground Link
💻 Code
declare function fnDerive<A extends unknown[], R>(
fn: (...args: A) => Generator<unknown, R>,
): unknown /** mocked return */;
fnDerive(function *(a = 0, b = '') {
console.log({
a,
// ^?
b
// ^?
});
});
🙁 Actual behavior
The type of a and b is unknown.
🙂 Expected behavior
The type of a and b should be number and string
Additional information about the issue
I was working with a library gensync when I hit this issue, and I extracted the minimum reproduction out of it.
Note that if I don't use default parameters, typescript can infer the a and b types correctly:
declare function fnDerive<A extends unknown[], R>(
fn: (...args: A) => Generator<unknown, R>,
): unknown /** mocked return */;
fnDerive(function *(a?: number | undefined, b?: string | undefined) {
console.log({
a,
// ^?
b
// ^?
});
a ??= 0;
b ??= '';
console.log({
a,
// ^?
b
// ^?
});
});
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンク先の TypeScript Playground と最小限の fnDerive 再現例から始め、ジェネリック関数の推論中にデフォルトパラメーターがどのように処理されるかを追跡します。例で a が number、b が string と推論され、同等の明示的なオプショナルパラメーターの例が引き続き正しく動作すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 42/100