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

Conditional type is ignored and never appears when using never as a parameter of generic type #38630

Closed
whatasoda opened this issue May 18, 2020 · 2 comments

Comments

@whatasoda
Copy link

whatasoda commented May 18, 2020

I love TypeScript, I appreciate that you provide us a great development experience.
But I found a strange behavior. I think it's a bug because I remember it worked correctly two years ago.

I want to use never as a type parameter that expects a union type of values of tuple-like array, in order to express that nothing is specified.
So I tried to use conditional type to handle never like:

type MyGeneric<T> = T extends never ? PlaceholderType : OtherTypeTransform<T>;

But it doesn't work correctly. If I set never to the parameter T, the result is always never, not PlaceholderType.

I understand I can use null or undefined instead. But actual values are stored in array and I don't want to use such values in the array. I mean, this issue happens when I assign [].

TypeScript Version: 3.9.2, (also 3.8.3)

Search Terms:

I tried searching whether a similar issue has already raised with these keywords is:issue never type parameter "extends never" conditional "<never>" .

Code

type MyManual0 = never extends never ? true : false; // true 👍
type MyManual1 = unknown extends never ? true : false; // false 👍

type MyGeneric<T> = T extends never ? true : false;

type MyType0 = MyGeneric<never>; // never 🤔
type MyType1 = MyGeneric<unknown>; // false 👍
type MyType2 = MyGeneric<any>; // boolean 👍

Expected behavior:

MyType0 becomes true as well as MyManual0

Actual behavior:

MyType0 becomes never

Playground Link:
https://www.typescriptlang.org/play?#code/C4TwDgpgBAsiMEMB2BXBAbADFAvFJEAbhAE5QQAewESAJgM75GlQD8UwJK0AXFAGYZ6EANxQA9OI5dogXg3AsjsAoUJFjxkadAEZcUFEgDWSAPYB3JOSo0GTYmXaduUPoPTCxkgULlLl4aHAA4jSkAJYAxgA8ACoAfLrRltR0jAR2bNJOLt4iin6qcNH+2HhBISQRkWmksR5S1WSAfBuAKLv5ASBFkDqlIMEEFVH6RmZItRJSrsJQCm1qnRAATLpl-ZXIIGOeAEbGxugQyNPyQA
Related Issues:

@jack-williams
Copy link
Collaborator

This is working correctly, but does cause some confusion. It is covered in the FAQ here:

Common bugs that aren't bugs.

The FAQ links through to issue #31751.

@whatasoda
Copy link
Author

whatasoda commented May 18, 2020

Sorry, I didn't read the page. 🙇‍♂️ I'll be careful about it in next time.💪

I understand the reason. I agree that it should be resolved to never. 🙂
Also, it seems that the issue you linked helps me to solve my problem.
Thank you so much!

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

2 participants