microsoft / microsoft/TypeScript
Two way assignability condition with generic params is not working as expected
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
assignability, ternary, conditions, generics
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
Playground link with relevant code
💻 Code
type NumberIfExtends<T, U> = T extends U ? U extends T ? number : string : string;
function f<G>() {
const x: NumberIfExtends<G, G> = 1;
}
🙁 Actual behavior
The value 1 is not assignable to variable x, with the following error: Type 'number' is not assignable to type 'NumberIfExtends<G, G>'.
🙂 Expected behavior
This should be assignable, since the the value passed to both T and U generic params is the same.
Please note the following very similar examples, that works without errors.
This is to show this is a minimal reproduction, and that the different do not justify the error:
type NumberIfExtends<T, U> = T extends U ? U extends T ? number : string : string;
const x: NumberIfExtends<'some type', 'some type'> = 1; // if I pass an explicit type (Not a generic)
type NumberIfExtends<T, U> = T extends U ? number : string; // if I use the condition in only one way.
function f<G>() {
const x: NumberIfExtends<G, G> = 1;
}
function f<G>(): void {
const x: G extends G ? G extends G ? number : string : string = 1; // if I define the type without an alias. This is not always possible if instead of defining a variable I would be passing the value to a function who defines its own type
}
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 and the minimal NumberIfExtends<G, G> reproduction. Trace how conditional types with generic parameters are checked for assignability, comparing the alias form with the inline and one-way examples. Done means assigning 1 to the generic conditional type succeeds without the reported error.
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
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100