Skip to content
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

Issue with keyof passed to a generic with conditional types #47159

Closed
sharshuv-quotient opened this issue Dec 15, 2021 · 3 comments
Closed

Issue with keyof passed to a generic with conditional types #47159

sharshuv-quotient opened this issue Dec 15, 2021 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@sharshuv-quotient
Copy link

sharshuv-quotient commented Dec 15, 2021

EDIT: I wrote this completely wrong, please see comments

Bug Report

🔎 Search Terms

Issue with keyof passed to a generic with conditional types (Sorry can't think of a better description)

🕗 Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

💻 Code

function f<G>(x: G): [G] extends [object] ? G : G {
  return x;
}

or:

function f<G>(x: G): G extends object ? G : G {
  return x;
}

🙁 Actual behavior

Fails with the following error:

Type 'G' is not assignable to type '[G] extends [object] ? G : G'

🙂 Expected behavior

G should be assignable to [G] extends [object] ? G : G.

Additional clues

Without condition it works

function f<G>(x: G): G {
  return x;
}

With condition that is always true it works:

function f<G>(x: G): [G] extends [any] ? G : G {
  return x;
}

With a condition that is always false it doesn't work:

function f<G>(x: G): [G] extends [never] ? G : G {
  return x;
}
@fatcerberus
Copy link

This is the behavior in every version I tried

Your playground is set to TS 3.5.1. Using 4.3.5 or higher makes the error go away, so this is apparently already fixed:

That being said, I have to question why someone would use a conditional type at all if they know in advance it will always take the same branch.

Also:

Issue with keyof passed to a generic with conditional types

There's no keyof in any of your examples.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Dec 15, 2021
@sharshuv-quotient
Copy link
Author

sharshuv-quotient commented Dec 16, 2021

This is embarrassing.
I was trying different versions to make sure that this wasn't a regression, then I found out I can simplify the minimal-reproduction even more. Apparently, with the latest version, this is the minimal reproduction of my original problem:
https://www.typescriptlang.org/play?#code/C4TwDgpgBAsiAq4IB4DiA+KBeKqoQA9gIA7AEwGcoBDEkKAflygC5cBuAKE4DMBXEgGNgASwD2JKDzToAFATYBrCCDE9cASjZxEkZMtXqMUAN6coUAE4RgfS5IJcAvpyA

type MyType<G> = [G] extends [object] ? G : G;

function f<G>(x: keyof G): MyType<keyof G> {
  return x;
}

I expect keyof G to be assianble to MyType<keyof G> as MyType is basically an identity type.
Should I create a new issue and close this one or edit this issue accordingly?

@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants