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

extends within dt function is not safe? #46779

Closed
loynoir opened this issue Nov 11, 2021 · 5 comments
Closed

extends within dt function is not safe? #46779

loynoir opened this issue Nov 11, 2021 · 5 comments

Comments

@loynoir
Copy link

loynoir commented Nov 11, 2021

Bug Report

🔎 Search Terms

extends dt function

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

Playground link with relevant code

💻 Code

    type dtExtends<X, Y> = X extends Y ? true : false

    // Expected `true`, but actually it's `never`
    type y = dtExtends<never, never>

🙁 Actual behavior

never extends never is true.

But _dtExtends<never, never> is not true, but never.

  {
    'describe: extends within dt function is unsafe'

    type _dtExtends<X, Y> = X extends Y ? true : false

    'describe: never extends never'
    // $ExpectType true
    type x = never extends never ? true : false

    'describe: unexpectedly, never is not extends never within dt function
    // Expected `true`, but actually it's `never`
    // $ExpectType never
    type y = _dtExtends<never, never>

  }

🙂 Expected behavior

never extends never is true.

_dtExtends<never, never> is true.

  {
    'describe: extends within dt function is unsafe'

    type _dtExtends<X, Y> = X extends Y ? true : false

    'describe: never extends never'
    // $ExpectType true
    type x = never extends never ? true : false

    'describe: never extends never within dt function'
    // $ExpectType true
    type y = _dtExtends<never, never>

  }
@JeroenoBoy
Copy link

try dtR<X, Y>() you currently use an Array for the first type, and nothing as the second.

@loynoir
Copy link
Author

loynoir commented Nov 11, 2021

try dtR<X, Y>() you currently use an Array for the first type, and nothing as the second.

@JeroenoBoy
Ah, that is not the problem, just add reference to avoid ... is declared but never used.

never extends never is true

The problem is _dtExtends<never, never> is not true, but never

🤔

@whzx5byb
Copy link

Duplicate of #23182 (comment)

@MartinJohns
Copy link
Contributor

MartinJohns commented Nov 11, 2021

Your conditional type distributes over the type arguments. But since never is an empty union, there's nothing to distribute over and you end up with never. If you disable distribution in the conditional type (as documented) you end up with true.

https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types

@loynoir
Copy link
Author

loynoir commented Nov 11, 2021

You guys are awesome!!!!!!!! 😘

@loynoir loynoir closed this as completed Nov 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants