microsoft / microsoft/TypeScript
`infer` not returning expected inner type if another inner type is used in the outer type result
Nobody has claimed this yet.
- 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
💻 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
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 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