microsoft / microsoft/TypeScript
Can't infer the type of this self-calculated
Open
Nobody has claimed this yet.
Needs Investigation
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
🔎 Search Terms
- infer
- self-calculated
- generic
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about Type Inference
⏯ Playground Link
type Exactify<T, X extends T> = T & {
[K in keyof X]: K extends keyof T ? X[K] : never
}
declare function pick<T, X extends Exactify<{
[K in keyof T]?: true
}, X>>(t: T, x: X): [Pick<T, keyof X>,...
💻 Code
type Exactify<T, X extends T> = T & {
[K in keyof X]: K extends keyof T ? X[K] : never
}
declare function pick<T, X extends Exactify<{
[K in keyof T]?: true
}, X>>(t: T, x: X): [Pick<T, keyof X>, X, X extends Exactify<infer A, infer B> ? B : never]
const t0 = pick({ a: '1', b: 1 }, { a: true })
const [
a0,
a1,
// ^? Exactify<{ a?: string | undefined; b?: number | undefined }, { a: boolean }>
a2
// ^? { a?: true | undefined; b?: true | undefined }
] = pick({ a: '1', b: 1 }, { a: true })
🙁 Actual behavior
The a2 variable is { a?: true | undefined; b?: true | undefined } type.
🙂 Expected behavior
Infer the a2 variable is { a: boolean } or { a: true } type.
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
Start with the linked TypeScript Playground reproduction and the generic pick/Exactify example in the issue. Investigate why the inferred a2 type is Exactify<...> rather than { a: boolean } or { a: true }, then verify the expected inference with a focused compiler test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100