microsoft / microsoft/TypeScript
Type argument inference fails using partial generic type when it should be possible
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
When trying to infer a type argument when the argument is used as a generic to another type which itself is nested inside Partial<...>, type inference fails.
🔎 Search Terms
type inference fail partial generic
type inference error partial generic
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about inference.
⏯ Playground Link
Playground link with relevant code
💻 Code
type Target = 'mongodb' | 'jsonSchema7' | 'openApi3'
interface NormalProps<TTarget extends Target> {
target: TTarget
normal: number
}
interface MongoProps {
target: Extract<Target, 'mongodb'>
mongo: number
}
type Props<TTarget extends Target = 'jsonSchema7'> = TTarget extends Exclude<Target, 'mongodb'>
? NormalProps<TTarget>
: MongoProps;
const t = <TTarget extends Target = 'jsonSchema7'>(props: Partial<Props<TTarget>>) => {}
t({ target: 'openApi3' })
🙁 Actual behavior
Type inference fails, error is displayed: Type '"openApi3"' is not assignable to type '"jsonSchema7"'.. When manually passing the generic - t<'openApi3'>({ target: 'openApi3' }) - everything works fine.
🙂 Expected behavior
Type inference should be successful, as one of the discriminating properties (target) is set.
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 by reproducing the inference failure in the linked TypeScript Playground using the provided generic types and call, then compare it with the explicit t<'openApi3'> call. Investigate the compiler's type argument inference for the Partial<Props<TTarget>> parameter; done means the inferred call accepts target: 'openApi3' without an error while preserving the expected generic behavior.
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
- 42/100