Last active
July 27, 2022 12:44
-
-
Save fostyfost/669588d4ab0cf551c96165b28e3eefe3 to your computer and use it in GitHub Desktop.
IsAny TS helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Checks if type `T` is the `any` type. | |
* @see https://stackoverflow.com/a/49928360/3406963 | |
* @see https://github.com/dsherret/conditional-type-checks/blob/main/mod.ts | |
*/ | |
type IsAny<T> = 0 extends 1 & T ? true : false | |
type Example<S> = IsAny<S> extends false ? 'ok' : never | |
/** | |
* Return True if `T` is `any`, otherwise return `False`. | |
* @see https://github.com/joonhocho/tsdef | |
*/ | |
type IsAny<T, True, False = never> = | |
// test if we are going the left AND right path in the condition | |
true | false extends (T extends never ? true : false) ? True : False | |
type Example<S> = IsAny<S, 'ok', never> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment