Standard library types
npm install @rcompat/typeTypeScript utility types for JavaScript runtimes.
A collection of TypeScript utility types for type-level programming. Includes
type predicates, transformations, and common type aliases. Works consistently
across Node, Deno, and Bun.
``bash`
npm install @rcompat/type
`bash`
pnpm add @rcompat/type
`bash`
yarn add @rcompat/type
`bash`
bun add @rcompat/type
`ts
import type { Dict, Maybe, Nullish, Primitive } from "@rcompat/type";
// Dict
const config: Dict
// Maybe
function find(id: number): Maybe
return users.get(id);
}
// Nullish = null | undefined
function isNullish(value: unknown): value is Nullish {
return value == null;
}
// Primitive = bigint | boolean | null | number | string | symbol | undefined
function isPrimitive(value: unknown): value is Primitive {
return value !== Object(value);
}
`
`ts
import type { IsAny, IsNever, IsUnion, IsTuple, IsArray } from "@rcompat/type";
// check if type is any
type A = IsAny
type B = IsAny
// check if type is never
type C = IsNever
type D = IsNever
// check if type is a union
type E = IsUnion
type F = IsUnion
// check if type is a tuple
type G = IsTuple<[string, number]>; // true
type H = IsTuple
// check if type is an array
type I = IsArray
type J = IsArray<[string]>; // true (tuples are arrays)
`
`ts
import type {
Mutable,
DeepMutable,
Not,
UnionToTuple,
TupleToUnion,
} from "@rcompat/type";
// remove readonly modifier
type Writable = Mutable
// { a: string }
// recursively remove readonly
type DeepWritable = DeepMutable<
Readonly<{ nested: Readonly<{ value: number }> }>
>;
// { nested: { value: number } }
// boolean negation
type Yes = Not
type No = Not
// convert union to tuple
type Tuple = UnionToTuple<"a" | "b" | "c">;
// ["a", "b", "c"]
// convert tuple to union
type Union = TupleToUnion<["a", "b", "c"]>;
// "a" | "b" | "c"
`
`ts
import type { Newable, UnknownFunction } from "@rcompat/type";
// constructor type
function createInstance
return new Ctor();
}
// generic function signature
function wrap(fn: UnknownFunction) {
return (...args: unknown[]) => fn(...args);
}
`
`ts
import type { JSONValue, Serializable } from "@rcompat/type";
// valid JSON value
const data: JSONValue = {
name: "Bob",
age: 30,
active: true,
tags: ["admin", "user"],
};
// implement custom serialization
class User implements Serializable {
constructor(private id: number, private name: string) {}
toJSON(): JSONValue {
return { id: this.id, name: this.name };
}
}
`
`ts
import type { MaybePromise } from "@rcompat/type";
// value or Promise of value
async function process(input: MaybePromise
const value = await input;
return value.toUpperCase();
}
process("hello"); // works
process(Promise.resolve("hello")); // also works
`
| Type | Definition |
| ---------------- | ---------------------------------------------------------------------- |
| Dict | Record |PartialDict
| | Partial |Entry
| | [K, T] tuple |EmptyObject
| | Empty object {} |Maybe
| | T \| undefined |Nullish
| | null \| undefined |Primitive
| | bigint \| boolean \| null \| number \| string \| symbol \| undefined |Boolish
| | "true" \| "false" |Some
| | true if any element in boolean tuple is true |
| Type | Returns true when... |IsAny
| -------------- | ----------------------- |
| | T is any |IsNever
| | T is never |IsUnknown
| | T is unknown |IsVoid
| | T is void |IsUnion
| | T is a union type |IsArray
| | T is an array type |IsTuple
| | T is a tuple type |IsClass
| | T is a class instance |
| Type | Description |
| ------------------------ | -------------------------------------- |
| Mutable | Remove readonly modifier |DeepMutable
| | Recursively remove readonly |Not
| | Boolean negation (true ↔ false) |UnionToTuple
| | Convert union to tuple type |TupleToUnion
| | Convert tuple to union type |UndefinedToOptional
| | Make undefined properties optional |Unpack
| | Flatten/expand type for better display |
| Type | Definition |
| ----------------------- | ----------------------------------- |
| Newable | new (...args: A) => I |AbstractNewable
| | Abstract constructor type |UnknownFunction
| | (...params: unknown[]) => unknown |
| Type | Description |
| -------------- | ---------------------------------------------- |
| JSONValue | Valid JSON types (primitives, arrays, objects) |Serializable
| | Interface with toJSON(): JSONValue method |JSONPointer
| | JSON pointer path type |
| Type | Definition |
| ----------------- | ----------------- |
| MaybePromise | T \| Promise |
| Type | Description |
| ------------- | --------------------------------------------- |
| Print | Convert type to string literal representation |TypedArray
| | Union of all typed array types |Join
| | Join string tuple with delimiter |StringLike
| | string \| { toString(): string } |StringClass
| | Interface for string-like classes |Printable
| | Interface with print(): string method |Import
| | Dynamic import result type |
`ts
import type { Dict, UnknownFunction } from "@rcompat/type";
type Events = Dict
class Emitter
#events: Partial
on
(this.#events[event] ??= [] as E[K]).push(handler);
}
}
`
`ts
import type { IsUnion, UnionToTuple } from "@rcompat/type";
type UnionLength = UnionToTuple["length"];
type A = UnionLength<"a" | "b" | "c">; // 3
type B = UnionLength
`
`ts
import type { JSONValue } from "@rcompat/type";
function parseJSON(text: string): JSONValue {
return JSON.parse(text);
}
function stringify(value: JSONValue): string {
return JSON.stringify(value);
}
`
`ts
import type { UndefinedToOptional } from "@rcompat/type";
type Input = {
required: string;
optional: string | undefined;
};
type Output = UndefinedToOptional;
// { required: string; optional?: string }
``
| Runtime | Supported |
| ------- | --------- |
| Node.js | ✓ |
| Deno | ✓ |
| Bun | ✓ |
No configuration required — just import and use.
MIT
See CONTRIBUTING.md in the repository root.