# Type Guards Source: [`lib/type-check.d.ts`](../../lib/type-check.d.ts) Every type below follows the same shape: ```ts type IfX = /* ... */; ``` It checks a condition about `T` and resolves to `Y` when the condition holds, `N` otherwise. `Y`/`N` default to the literal types `true`/`false`, which lets you use them directly as compile-time assertions: ```ts import type { IfNever } from 'ts-gems'; type Check = IfNever; // true type Check2 = IfNever; // false ``` ...or pass real types through `Y`/`N` to build a conditional transform: ```ts type OrString = IfNever; // replace `never` with `string` ``` ## The `*OrAny` variants `IfTupleOrAny`, `IfPrimitiveOrAny`, `IfObjectOrAny`, `IfFunctionOrAny`, and `IfClassOrAny` are convenience wrappers that treat `any` as an automatic match, since `any` would otherwise fail every structural check: ```ts type IfXOrAny = IfAny extends true ? Y : IfX; ``` ```ts import type { IfObject, IfObjectOrAny } from 'ts-gems'; type A = IfObject; // false - `any` doesn't structurally match "object" type B = IfObjectOrAny; // true ``` ## `IfAny` Detects the `any` type specifically — distinct from `unknown`, `never`, and every other type. Uses the well-known `0 extends 1 & T` trick, since `any` is the only type where this holds. ```ts import type { IfAny } from 'ts-gems'; type A = IfAny; // true type B = IfAny; // false type C = IfAny; // false type D = IfAny; // false ``` ## `IfNever` ```ts import type { IfNever } from 'ts-gems'; type A = IfNever; // true type B = IfNever; // false type C = IfNever; // false ``` ## `IfUndefined` / `IfNull` / `IfUnknown` Exact-type checks for `undefined`, `null`, and `unknown` respectively, each implemented via [`IfEquals`](#ifequalst1-t2-y-n). ```ts import type { IfNull, IfUndefined, IfUnknown } from 'ts-gems'; type A = IfUndefined; // true type B = IfUndefined; // false type C = IfNull; // true type D = IfNull; // false type E = IfUnknown; // true type F = IfUnknown; // false - `any` is not `unknown` ``` ## `IfNullish` `true` for `null` **or** `undefined`. ```ts import type { IfNullish } from 'ts-gems'; type A = IfNullish; // true type B = IfNullish; // true type C = IfNullish<0>; // false ``` ## `IfSymbol` `true` only for the general `symbol` type — **not** for a specific `unique symbol` (e.g. the type of a `const x = Symbol('x')`), which is a distinct, narrower type. ```ts import type { IfSymbol } from 'ts-gems'; const uniqueSym = Symbol('x'); type A = IfSymbol; // true type B = IfSymbol; // false - a *specific* unique symbol type C = IfSymbol; // false ``` ## `IfTuple` `true` for a fixed-length tuple (`[string, number]`, `[]`, ...); `false` for a variable-length array (`string[]`) or anything else. Distinguishes the two by checking whether `T['length']` is a literal number (tuple) or the general `number` type (array). ```ts import type { IfTuple } from 'ts-gems'; type A = IfTuple<[string, number]>; // true type B = IfTuple<[]>; // true type C = IfTuple; // false type D = IfTuple; // false ``` > This is the type that decides, throughout the whole library, whether a > tuple gets left untouched by `Deeper*` transforms instead of being > collapsed into a homogeneous array — see [the `Deep*`/`Deeper*` > convention](../api.md#the-deep--deeper-convention). ## `IfPrimitive` `true` for any [`Primitive`](types.md#primitive): `string | number | bigint | boolean | symbol | null | undefined`, plus `any`. `false` for objects, arrays, functions, and classes. ```ts import type { IfPrimitive } from 'ts-gems'; type A = IfPrimitive; // true type B = IfPrimitive; // true type C = IfPrimitive<{}>; // false type D = IfPrimitive; // false ``` ## `IfEmptyObject` `true` only for the exact type `{}`. ```ts import type { IfEmptyObject } from 'ts-gems'; type A = IfEmptyObject<{}>; // true type B = IfEmptyObject<{ x: 1 }>; // false type C = IfEmptyObject; // false ``` ## `IfObject` `true` for anything structurally an `object` that isn't a primitive, `Function`, or array (arrays get their own check via [`IfTuple`](#iftuplet-y-n) or a plain `T[]` test elsewhere in the library). ```ts import type { IfObject } from 'ts-gems'; type A = IfObject<{ x: 1 }>; // true type B = IfObject; // true type C = IfObject; // false type D = IfObject; // false type E = IfObject; // false ``` ## `IfFunction` `true` for any function type (including arrow functions), `false` for a constructor/class reference (see [`IfClass`](#ifclasst-y-n) for that case). ```ts import type { IfFunction } from 'ts-gems'; class Foo {} type A = IfFunction<() => void>; // true type B = IfFunction<(a: string) => boolean>; // true type C = IfFunction; // false - a constructor, not a plain function type D = IfFunction; // false - an instance ``` ## `IfClass` `true` when `T` is a constructor/class reference — i.e. it matches [`Type`](types.md#typet--any) (`new (...args: any[]) => any`). ```ts import type { IfClass } from 'ts-gems'; class Foo {} function bar() {} type A = IfClass; // true type B = IfClass; // false type C = IfClass; // false - an instance is not a constructor ``` ## `IfEquals` Exact, deeply-structural type equality — the classic [Matt McCutchen conditional-type trick](https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-421529650), extended in this library to also compare object shapes recursively. Two types are equal only if they are mutually and exactly assignable, including modifiers (`?`, `readonly`). ```ts import type { IfEquals } from 'ts-gems'; type A = IfEquals; // true type B = IfEquals; // false type C = IfEquals<{ x: 1 }, { x?: 1 }>; // false - optionality differs type D = IfEquals; // true type E = IfEquals; // false ``` ## `IfCompatible` A looser, one- or two-way _assignability_ check (unlike `IfEquals`, which requires exact equality). Used internally to compare function parameter/return compatibility, and handles `any`/`unknown`/`never`/`null`/`undefined` specially so they behave as you'd expect at the call site rather than by strict structural rules. ```ts import type { IfCompatible } from 'ts-gems'; type A = IfCompatible; // true type B = IfCompatible; // true type C = IfCompatible<{}, number>; // false type D = IfCompatible<() => void, (a: string) => void>; // true ```