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

未關閉
#54,622 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
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 小時
30 天內合併 PR
117

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

microsoft/TypeScript 的其他 Issue

查看 microsoft/TypeScript 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。