microsoft / microsoft/TypeScript

Allow inferring return type within a type guard in conditional types

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

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

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

説明

Bug Report

🔎 Search Terms

typeguard conditional infer

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about (bug reporting)
⏯ Playground Link

Playground link with relevant code

💻 Code

This is simplified pretty heavily:

type InferredTypeGuard<TypeGuard> = 
    TypeGuard extends ((node: infer Input) => node is infer Output)
        ? (node: Input) => node is Output
        : never;
🙁 Actual behavior

Error on infer Output:

A type predicate's type must be assignable to its parameter's type.
  Type 'Output' is not assignable to type 'Input'.
    'Input' could be instantiated with an arbitrary type which could be unrelated to 'Output'.(2677)
🙂 Expected behavior

TypeScript should allow a conditional type to infer the Output type within the type guard function's return type.

There's no other syntax I can find to do so, and I would have thought this would be the cleanest syntax. Not sure if bug report or feature request...

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

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

はじめの一歩

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

調査の方向性

リンクされた TypeScript Playground の例と、infer Input および infer Output を使用する単純化された条件型から始めます。エラー 2677 を報告する型述語の代入可能性チェックを追跡し、条件型の推論がそれとどのように相互作用すべきかを判断します。例が期待される推論後の入力型と出力型で型チェックを通過し、この動作に対するリグレッションカバレッジが用意されていれば完了です。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

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

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