microsoft / microsoft/TypeScript

Two way assignability condition with generic params is not working as expected

Open
#44,975 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Conditional Types Needs Human Review
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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.