Skip to content

Instantly share code, notes, and snippets.

@fostyfost
Last active July 27, 2022 12:44
Show Gist options
  • Save fostyfost/669588d4ab0cf551c96165b28e3eefe3 to your computer and use it in GitHub Desktop.
Save fostyfost/669588d4ab0cf551c96165b28e3eefe3 to your computer and use it in GitHub Desktop.
IsAny TS helper
/**
* 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