Generic argument narrowed from `T | undefined` collapses a dependent conditional return type to `never`/`undefined`
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 45/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- typescript
- 領域
- compilers
研究方向
Start by running the supplied TypeScript reproduction in the Playground and compare the inferred types with and without the truthy narrowing. Trace the generic narrowing and conditional-type evaluation involved in the get call; done means the narrowed call preserves the value type and reports number | undefined rather than plain undefined, with a regression test covering the case.
由索引模型根據 Issue 內容生成。
描述
Bug Report
- This is a bug, not a question.
TypeScript Version
Reproduces on 6.0.3 and on 7.0.2 (npx typescript@latest, checked 2026-09-22) — not a recent regression, present in both.
Search Terms
narrowing generic T & {}, distributive conditional type never, naked type parameter
narrowed loses distributivity, generic argument narrowed conditional return type never,
extends infer R generic narrowing never
Code
type PlainObject<T> = Exclude<T, string | number | boolean | null | undefined> & object;
type Keys<T> = PlainObject<T> extends infer R ? keyof R : never;
type ValueAt<T, K extends Keys<T>> =
PlainObject<T> extends infer R ? (K extends keyof R ? R[K] : never) : never;
declare function get<const T, const K extends Keys<T>>(obj: T, key: K): ValueAt<T, K>;
// Works as expected: no narrowing involved, return type resolves to the real value type.
function withoutNarrowing<T, K extends Keys<T>>(obj: T, key: K) {
return get(obj, key);
}
// Bug: same call, but obj/key were first narrowed down from `T | undefined` via a truthy check.
function withNarrowing<T, K extends Keys<T>>(obj: T | undefined, key: K | undefined) {
if (!obj || !key) return;
return get(obj, key);
}
const ok = withoutNarrowing({ a: 1 }, 'a'); // number -- correct
const bug = withNarrowing({ a: 1 }, 'a'); // undefined -- wrong, should be `number | undefined`
Playground (paste the code above manually if this link doesn't load it correctly)
(The playground snippet includes two extra lines, checkOk/checkBug, that force the compiler to print each variable's real type in an error message — remove them, or just hover over ok/bug instead.)
Expected behavior
bug's type should be number | undefined — same as ok, plus undefined from the
early return. obj/key are proven non-nullable at the get(obj, key) call site (right
after the if (!obj || !key) return; guard), so the call should behave identically to
withoutNarrowing.
Actual behavior
bug's type collapses to plain undefined — the ValueAt<T, K> branch silently
resolves to never, so the union ValueAt<T, K> | undefined simplifies away the real
value type entirely. No error is raised anywhere; the incorrect type just propagates
silently, which is the dangerous part (a consumer reading bug's type has no signal
anything went wrong).
Root cause (as far as I could narrow it down): after if (!obj || !key) return;, obj
narrows to T & {} rather than to T (documented since TS 4.8, to stay sound about
generics that could structurally include null/undefined). T & {} is an intersection
type, not the naked type parameter symbol T anymore — and Keys<T>/ValueAt<T, K>
rely on T being naked for their X extends infer R ? ... : never conditionals to stay
deferred (unevaluated) until a concrete T is substituted. Once inference for the get
call has to work with T & {} (and its constraint, unknown), the checker seems to try
to evaluate the conditional eagerly instead of deferring it, and it collapses to never.
This only reproduces with the X extends infer R ? ... : never indirection present in
both Keys<T> and ValueAt<T, K> — a version without that indirection (e.g. a plain
T[K] lookup instantiated through a similarly-shaped generic call) resolves correctly
even after the same narrowing, so the infer R step specifically seems to be what tips
the checker into eager evaluation.
Workaround found
Passing the type arguments explicitly at the call site avoids the bug entirely, since it
sidesteps inference off the narrowed (T & {}) argument type altogether:
function withNarrowingFixed<T, K extends Keys<T>>(obj: T | undefined, key: K | undefined) {
if (!obj || !key) return;
return get<T, K>(obj, key); // <- explicit type args: back to `number | undefined`
}
Related Issues
- https://github.com/microsoft/TypeScript/issues/57246
- https://github.com/microsoft/TypeScript/issues/57707
- https://github.com/microsoft/TypeScript/issues/46975
- https://github.com/microsoft/TypeScript/issues/54806
- https://github.com/microsoft/TypeScript/issues/52327
Related in that they deal with narrowing + generic conditional types, but none is an exact duplicate of the scenario above, as far as I could tell.
- 主要語言
- Go
- 星號
- 111k
- 分支
- 16.4k
- 平均合併
- 1 天 11 小時
- 30 天內合併 PR
- 149
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
microsoft/TypeScript 的其他 Issue
-
Class names `eval` and `arguments` are not reported as invalid strict mode bindings可能已有人在做 @dmety 於 4 天前認領。 未關閉Bug
難度 2/5 1-3 小時 新手友好度 68/100
microsoft/TypeScript#64661 ·
維護者通常 1 天內回覆
-
Non-enum CLI options with multiple values separated by comma and space aren't whitespace trimmed可能已有人在做 @RyanCavanaugh 於 5 天前認領。 未關閉Bug
難度 2/5 1-3 小時 新手友好度 68/100
microsoft/TypeScript#64618 · 已指派 1 人 ·
維護者通常 1 天內回覆
-
TypeScript 7 VS Code extension: a workspace "typescript" 7.x package is not detected, only "@typescript/native-preview", which is no longer published可能已有人在做 @jakebailey 於 5 天前認領。 未關閉Needs Investigation
難度 2/5 1-3 小時 新手友好度 84/100
microsoft/TypeScript#64565 · 1 個 reaction · 已指派 1 人 ·
維護者通常 1 天內回覆
-
`EFNoLeadingComments` suppresses synthesized leading comments in tsgo; Strada only suppresses source comments可能已有人在做 @weswigham 於 12 天前認領。 未關閉Needs Investigation
難度 2/5 1-3 小時 新手友好度 84/100
microsoft/TypeScript#64453 · 1 則留言 · 已指派 1 人 ·
維護者通常 1 天內回覆
-
enhance: Add tests for premature caching of contextual parameter types可能已有人在做 @luchenxu73 於 26 天前認領。 未關閉Possible Improvement
難度 2/5 1-3 小時 新手友好度 78/100
microsoft/TypeScript#64278 · 1 則留言 · 1 個 reaction ·
維護者通常 1 天內回覆
查看 microsoft/TypeScript 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 66/100
AvengeMedia/DankMaterialShell#3761 ·
維護者通常 1 天內回覆
-
docs: clarify replacement for outdated Terms of Service URL in readme.md可能已有人在做 @mohitjoshi-hey 今天認領。 未關閉
難度 1/5 1-3 小時 新手友好度 66/100
-
難度 2/5 1-3 小時 新手友好度 72/100
SocialGouv/iterion#2320 ·
維護者通常 1 天內回覆
-
helm chart: the first upgrade after install restarts the filer and S3 gateway although nothing changed可能已有人在做 @egezamb 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆
-
[mesheryctl] Potential runtime panic (index out of range) in system status when pod container statuses are partially populated可能已有人在做 @Ayush-AM 今天認領。 未關閉component/mesheryctl kind/bug language/go
難度 2/5 1-3 小時 新手友好度 70/100
meshery/meshery#22312 · 2 則留言 ·
維護者通常 1 天內回覆