TS-UT

other

validation

IsUnknown

type IsUnknown<T>

Checks if a type is unknown.

IsAny

type IsAny<T>

Checks if a type is any.

IsNever

type IsNever<T>

Checks if a type is never.

IsSubtype

type IsSubtype<T, U>

An alias for T extends U ? true : false.

  • Used to chain multiple checks together.
  • Useful to reduce nesting level for complex types.
  • Can require some thinking before using it correctly.
  • See the last comment in the example below.
  • This type is mostly for personal use :)
Example
// Instead of
type Example<A, B> = A extends Dict ? B extends Dict ? "Do something" : never : never;

// You can do
type Version2<A, B> = IsSubtype<A, Dict> | IsSubtype<B, Dict> extends true ? "Do something" : never;

// Note that TypeScript doesn't know about these checks, which will likely cause errors saying A and B are not objects inside the "Do Something" branch.

IsNotSubtype

type IsNotSubtype<T, U>

A negated version of IsSubtype.

HasType

type HasType<U, T>

Checks whether a union contains a specific type.

Example
type R1 = HasType<"foo" | "bar", "foo">; // true
type R2 = HasType<"foo" | "bar", "baz">; // false

LacksType

type LacksType<U, T>

A negated version of HasType.

AreEqual

type AreEqual<A, B>

Checks if two types are equal. Not just that they extend each other, but are exactly the same (at least until a bug proves otherwise).

AreNotEqual

type AreNotEqual<A, B>

A negated version of AreEqual.

utils

Valid

type Valid

An alias for any.

Range

type Range

A tuple representing a start and end value.

Example
const ranges = elements.map<Range>((ele) => [ele.getStart(), ele.getEnd()]);

RORange

type RORange

A readonly version of Range.

Example
function foo(range: RORange) {}

NonNullish

type NonNullish<T>

Removes undefined and null from a type.

Nullish

type Nullish<T>

Adds undefined and null to a type.

Assert

type Assert<T extends E, E = true>
Example
// Suppose you have a type like this
type Token = "+" | "-" | "*" | "/";

// And you want to give each member an alias:
const tk = {
	plus: "+",
	minus: "-",
	times: "*",
	divide: "/",
} satisfies Dict<string, Token>;

// To make sure that all tokens have an alias, you can use something like this:
{
  type Check = Assert<AreEqual<Values<typeof tk>, Token>>;
}
// You'll see an error if there's a missing/extra token
// Note: AreEqual and Values are other utils provided by this package

Not

type Not<T extends boolean>

Negates a boolean.

SmartExtract

type SmartExtract<T, U>

Similar to the built-in Extract, except it return Extract<U, T> (or simply U) when T is unknown, any or a primitive type (string, number, boolean).

Self

type Self<T, I1 = TS_UT, I2 = TS_UT, I3 = TS_UT, I4 = TS_UT, I5 = TS_UT>

Useful for debugging.

On this page