Type argument inference fails using partial generic type when it should be possible

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

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

評価

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

調査の方向性

まず、提供されたジェネリック型と呼び出しを使って、リンク先の TypeScript Playground で推論の失敗を再現し、その後、明示的な t<'openApi3'> 呼び出しと比較します。Partial<Props<TTarget>> パラメーターに対するコンパイラーの型引数推論を調査します。推論された呼び出しが target: 'openApi3' をエラーなしで受け入れ、期待されるジェネリックの動作を維持できれば完了です。

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

説明

Domain: check: Type Inference Possible Improvement

Bug Report

When trying to infer a type argument when the argument is used as a generic to another type which itself is nested inside Partial<...>, type inference fails.

🔎 Search Terms

type inference fail partial generic
type inference error partial generic

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about inference.

⏯ Playground Link

Playground link with relevant code

💻 Code
type Target = 'mongodb' | 'jsonSchema7' | 'openApi3'

interface NormalProps<TTarget extends Target> {
  target: TTarget
  normal: number
}

interface MongoProps {
  target: Extract<Target, 'mongodb'>
  mongo: number
}

type Props<TTarget extends Target = 'jsonSchema7'> = TTarget extends Exclude<Target, 'mongodb'>
  ? NormalProps<TTarget>
  : MongoProps;

const t = <TTarget extends Target = 'jsonSchema7'>(props: Partial<Props<TTarget>>) => {}

t({ target: 'openApi3' })
🙁 Actual behavior

Type inference fails, error is displayed: Type '"openApi3"' is not assignable to type '"jsonSchema7"'.. When manually passing the generic - t<'openApi3'>({ target: 'openApi3' }) - everything works fine.

🙂 Expected behavior

Type inference should be successful, as one of the discriminating properties (target) is set.

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

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

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

はじめの一歩

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

microsoft/TypeScript のほかの issue

microsoft/TypeScript の issue をすべて見る

似ている issue

Go の issue をもっと見る

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

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