microsoft / microsoft/TypeScript

Can't infer the type of this self-calculated

Open
#54,766 0 comments 0 reactions 0 assignees View on GitHub

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>,...

Playground Link

💻 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.