microsoft / microsoft/TypeScript

Two way assignability condition with generic params is not working as expected

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

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

Bug Domain: Conditional Types Needs Human Review
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Bug Report

🔎 Search Terms

assignability, ternary, conditions, generics

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

Playground link with relevant code

💻 Code
type NumberIfExtends<T, U> = T extends U ? U extends T ? number : string : string;
function f<G>() {
  const x: NumberIfExtends<G, G> = 1;
}
🙁 Actual behavior

The value 1 is not assignable to variable x, with the following error: Type 'number' is not assignable to type 'NumberIfExtends<G, G>'.

🙂 Expected behavior

This should be assignable, since the the value passed to both T and U generic params is the same.

Please note the following very similar examples, that works without errors.
This is to show this is a minimal reproduction, and that the different do not justify the error:

type NumberIfExtends<T, U> = T extends U ? U extends T ? number : string : string;
const x: NumberIfExtends<'some type', 'some type'> = 1; // if I pass an explicit type (Not a generic)
type NumberIfExtends<T, U> = T extends U ? number : string; // if I use the condition in only one way. 
function f<G>() {
  const x: NumberIfExtends<G, G> = 1;
}
function f<G>(): void {
  const x: G extends G ? G extends G ? number : string : string = 1; // if I define the type without an alias. This is not always possible if instead of defining a variable I would be passing the value to a function who defines its own type
}

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

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

はじめの一歩

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

調査の方向性

リンクされている TypeScript Playground と最小限の NumberIfExtends<G, G> の再現コードから始めてください。ジェネリックパラメーターを持つ条件型の代入可能性がどのようにチェックされるかを追跡し、エイリアス形式とインラインおよび一方向の例を比較してください。報告されているエラーなしで、ジェネリック条件型に 1 を代入できれば完了です。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
48/100

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

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