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

Get type should be able to return never so it can be used in a conditional type #315

Closed
joseDaKing opened this issue Nov 10, 2021 · 5 comments

Comments

@joseDaKing
Copy link

joseDaKing commented Nov 10, 2021

Get type should be able to return never so it can be used in a conditional type or a new generic type could be created.

type A<T> = Get<T, "items"> extends unkown ? "true" : "false"; //Will allways yield true
@sindresorhus
Copy link
Owner

@asbjornh Thoughts on this?

@asbjornh
Copy link
Contributor

Yeah! The extends check in the above example is in the wrong order. The following is likely what OP is looking for:

type A<T> = unknown extends Get<T, "items"> ? true : false;

Explanation:
Conditional types can make a bit more intuitive sense if you read extends as "is a subtype of" or "is assignable to".
F. ex., the above example is asking the question "is Get<T, "items"> assignable to unknown?". Since all types in TS are assignable to unknown, the answer is yes, no matter what the result of Get<T, "items"> is.

If you flip it you get the question "is unknown assignable to Get<T, "items">?". Since unknown is only assignable to unknown and any (at least to my knowledge), the answer to this question is true if Get<T, "items"> evaluates to unknown or any, or false otherwise.

@asbjornh
Copy link
Contributor

@joseDaKing Did the above resolve your issue? Can this be closed?

@joseDaKing
Copy link
Author

It resolved the issue thanks

@asbjornh
Copy link
Contributor

Great! 😄

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

3 participants