microsoft / microsoft/TypeScript
Computed type key inference inconsistent with identical explicitly declared type
Open
Nobody has claimed this yet.
Bug
Domain: Mapped Types
Help Wanted
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
🔎 Search Terms
keyof required keys key evaluation reduce getReducedType
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 Code
type distilled = distill<{
// ^?
foo: "default"
bar?: number
}>
// incorrectly inferred as unknown
type requiredDistilledKey = requiredKeyOf<distilled>
// ^?
// explicit declaration identical to hover of distilled
type declared = {
bar?: number
} & {
foo?: "default"
}
// now correctly inferred as never
type requiredDeclaredKey = requiredKeyOf<declared>
// ^?
type requiredKeyOf<o> = {
[k in keyof o]-?: o extends { [_ in k]-?: o[k] } ? k : never
}[keyof o]
type distill<t> = t extends object ? distillMappable<t> : t
type distillMappable<o> = {
[k in keyof o as k extends inferredDefaultKeyOf<o> ? never : k]: distill<o[k]>
} & {
[k in inferredDefaultKeyOf<o>]?: distill<o[k]>
}
type inferredDefaultKeyOf<o> = {
[k in keyof o]: o[k] extends "default" ? k : never
}[keyof o]
🙁 Actual behavior
requiredDistilledKey inferred as unknown
🙂 Expected behavior
requiredDistilledKey inferred as never, consistent with the equivalent declared version
Additional information about the issue
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the discrepancy in the linked TypeScript Playground using the provided distill, requiredKeyOf, and requiredDeclared examples. Trace how the computed mapped/intersection type is reduced and how requiredKeyOf evaluates it; done means requiredDistilledKey is inferred as never, matching requiredDeclaredKey.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100