microsoft / microsoft/TypeScript

`infer` not returning expected inner type if another inner type is used in the outer type result

Open
#54,213 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: Conditional Types Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

Given a generic outer type with 2 or more inner types, e.g. Foo<A, B>, that does an intersection involving multiple inner types, then infer won't return a specific inner type, it will return the intersection of the desired type plus the entire outer type. This is difficult for me to explain since I don't know the correct terms for everything, so please see the example.

🔎 Search Terms

infer intersection

🕗 Version & Regression Information
  • This changed between versions 4.1.5 and 4.2.3
⏯ Playground Link

playground link

💻 Code
type Foo<T extends {}, N extends string> = T & {
  bar: number;
  asdf: N;
};

interface A {
  things: number;
}

type MyFoo = Foo<A, 'asdf'>;

type InnerFromFoo<F> = F extends Foo<infer I, string> ? I : never;

type inner = InnerFromFoo<MyFoo>;

const a: inner = {
  things: 23
};
🙁 Actual behavior

The type inner is the intersection of A and { bar: number, asdf: string }

🙂 Expected behavior

inner should just be A

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 example and the generic conditional type InnerFromFoo<F>. Compare the inferred inner type with the expected A result across the reported TypeScript versions, then verify that the example infers only the inner type rather than an intersection with the outer type.

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
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.