Skip to content

Generic functions combining a conditional type and an intersection are not assignable to themselves #51399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ehmicky opened this issue Nov 3, 2022 · 4 comments · Fixed by #51405
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@ehmicky
Copy link

ehmicky commented Nov 3, 2022

🔎 Search Terms

"same type assignable"
"Two different types with this name exist, but they are unrelated."

🕗 Version & Regression Information

Please keep and fill in the line that best applies:

  • This is a crash
  • This is the behavior in every version I tried (3.3.3 to nightly), and I reviewed the FAQ for entries about it

⏯ Playground Link

Playground link with relevant code

💻 Code

declare let a: <T>() => (T extends true ? true : false) & boolean
declare let b: <T>() => (T extends true ? true : false) & boolean
a = b

🙁 Actual behavior

This produces the following error message:

Type '<T>() => (T extends true ? true : false) & boolean' is not assignable to 
type '<T>() => (T extends true ? true : false) & boolean'. 
Two different types with this name exist, but they are unrelated.
  Type 'false' is not assignable to type '(T extends true ? true : false) & boolean'.

Also, the following unexpectedly removes the error message:

  • Removing & boolean
  • Replacing T extends true ? true : false by true
  • Replacing T extends true ? true : false by T extends true ? true : true
  • Replacing b's type by typeof a

🙂 Expected behavior

Since the two types are identical, a and b should be assignable to each other.

@ahejlsberg ahejlsberg self-assigned this Nov 4, 2022
@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Nov 4, 2022
@ahejlsberg ahejlsberg added this to the TypeScript 5.0.0 milestone Nov 4, 2022
@ahejlsberg
Copy link
Member

Hmm, yeah, this looks like an issue in type inference between similar intersections.

@ehmicky
Copy link
Author

ehmicky commented Nov 5, 2022

Thanks a lot @ahejlsberg for the quick fix! 🚀

@FrameMuse
Copy link

Sorry for a bit unrelated comment (it's too long to search for similar issues and I'm too lazy to create a new one), but similar thing happening here when I try to return 1 and 2. It throws error saying Type '1' is not assignable to type 'Options["enabled"] extends true ? 1 : 2'.ts(2322).

function query<Options extends { enabled?: boolean }>(options?: Options): Options["enabled"] extends true ? 1 : 2 {
  if (options?.enabled) {
    return 1
  }
 
  return 2
}

@RyanCavanaugh
Copy link
Member

@FrameMuse see #33912

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants