microsoft / microsoft/TypeScript
Allow inferring return type within a type guard in conditional types
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
🔎 Search Terms
typeguard conditional infer
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about (bug reporting)
⏯ Playground Link
Playground link with relevant code
💻 Code
This is simplified pretty heavily:
type InferredTypeGuard<TypeGuard> =
TypeGuard extends ((node: infer Input) => node is infer Output)
? (node: Input) => node is Output
: never;
🙁 Actual behavior
Error on infer Output:
A type predicate's type must be assignable to its parameter's type.
Type 'Output' is not assignable to type 'Input'.
'Input' could be instantiated with an arbitrary type which could be unrelated to 'Output'.(2677)
🙂 Expected behavior
TypeScript should allow a conditional type to infer the Output type within the type guard function's return type.
There's no other syntax I can find to do so, and I would have thought this would be the cleanest syntax. Not sure if bug report or feature request...
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 simplified conditional type using infer Input and infer Output. Trace the type-predicate assignability check that reports error 2677 and determine how conditional-type inference should interact with it. Done means the example type-checks with the expected inferred input and output types, with regression coverage for the behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100