Skip to content
This repository has been archived by the owner on Nov 5, 2022. It is now read-only.

The way to check a type for being any. #8

Closed
Veetaha opened this issue Apr 8, 2019 · 3 comments
Closed

The way to check a type for being any. #8

Veetaha opened this issue Apr 8, 2019 · 3 comments

Comments

@Veetaha
Copy link
Contributor

Veetaha commented Apr 8, 2019

I've just read your README.md and haven't even downloaded your package yet, but I see you are confused stating the following

But it is not possible to shrink from any to any other types, because there is no way to know if a type is any or not. Conditional type expression T extends any and any extends T are always true.

There actually is a way to check for any type.
Type expression boolean extends (T extends never ? true : false) ? true : false evaluates to true only when T is of any type, so that both true and false branch in the inner conditions are matched and their union (namely boolean) is returned, which you ultimatelly check in the outer condition. You may use this definition or get an existing one from ts-typedefs package that exposes such handy definitions as Is[Not]Any<>, Is[Not]Unknown<>, Is[Not]Never<> and much more. Here is the code for IsAny<> type.

@hjkcai
Copy link
Owner

hjkcai commented Apr 8, 2019

Wow the repo ts-typedefs is awesome! Thanks a lot!!

In fact I already know how to check any and I am using the similar mechanism as you provide. Here is the code: https://github.com/hjkcai/typesafe-joi/blob/feature/split-files/test/util.ts . But your implementation is much smarter and can be used in more situations.

However, I am stuck in microsoft/TypeScript#30794 now. I am trying another way to implement typesafe-joi with (maybe) less type recursion.

BTW do you know how to force TypeScript to expand some complex deferred type? The evaluation of mapped types are sometimes (I don't know the exact point) deferred. Hovering on this kind of type looks horrible.

@Veetaha
Copy link
Contributor Author

Veetaha commented Apr 8, 2019

@hjkcai Thank you too))

However, I am stuck in microsoft/TypeScript#30794 now

Same issue with heap-overflow here. I can't generate enough instances of the type without crashing the tsc. I tried to create my own type-safe schema validator, and stopped at writing documentation, but I think there is no need for it while this package exists)

BTW do you know how to force TypeScript to expand some complex deferred type?

Unfortunately, I've never wondered about it. Most of the time complex types are expanded in-place when I hover over them, but not always. I didn't pay much attention to it because I consider forcing them expand to be impossible and not that important. Maybe you should create a question issue at Microsoft/TypeScript and/or Stackoverflow, it would be interesting also for me to get an answer from developers.

@hjkcai
Copy link
Owner

hjkcai commented May 10, 2019

@Veetaha I have published typesafe-joi 2.0.0 alpha version, which uses IsAny in this situation. And I am sure it is more stable now (I don't want to submit an issue to TypeScript about typesafe-joi anymore). Please have a try! If you have any idea or found any bug, feel free to open another issue 😀

@hjkcai hjkcai closed this as completed May 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants