Shared TypeScript utilities packaged as focused subpath modules.
npm i ts-utility-kitUse this when you want to install the latest published version from the npm registry.
npm i github:ShionTerunaga/ts-utility-kit#releaseUse this when you want to install directly from the built release branch on GitHub.
To pin a specific version, install from a version tag instead of release.
npm i github:ShionTerunaga/ts-utility-kit#v1.4.0This package exposes subpath modules. Import from the feature you need.
import { ValidationError } from "ts-utility-kit/error";
import { omitElementObject } from "ts-utility-kit/object";
import { createSome } from "ts-utility-kit/option";Available subpath exports:
ts-utility-kit/errorts-utility-kit/ists-utility-kit/mergerts-utility-kit/objectts-utility-kit/optionts-utility-kit/resultts-utility-kit/types
Use this when you want application errors with consistent names, codes, status values, or metadata.
import { BadRequestError, NotFoundError, SchemeError, ValidationError } from "ts-utility-kit/error";
throw new ValidationError({
field: "email",
issues: [{ path: "email", message: "Invalid format" }],
});
throw new SchemeError({
allowedSchemes: ["https"],
receivedScheme: "http",
});
throw new NotFoundError({
details: { resource: "user", id: "42" },
});Included exports:
BaseErrorBaseHttpError- HTTP error classes such as
BadRequestError,UnauthorizedError,ForbiddenError,NotFoundError,ConflictError,PayloadTooLargeError, andUnsupportedMediaTypeError SchemeErrorValidationError
Use these small type guards when narrowing nullable values.
import { isNull, isUndefined } from "ts-utility-kit/is";
function normalize(value: unknown) {
if (isNull(value) || isUndefined(value)) {
return "empty";
}
return String(value);
}Use this when you want to merge class name arrays while removing empty entries and duplicates.
import { classMerger } from "ts-utility-kit/merger";
const className = classMerger(["button", "", "button", "primary"]);
// "button primary"Use this when you want to copy an object without specific keys.
import { omitElementObject } from "ts-utility-kit/object";
const user = {
id: 1,
name: "Shion",
password: "secret",
};
const safeUser = omitElementObject(user, ["password"]);Use this when you want to represent nullable values explicitly as Some or None.
import { createNone, createSome, isNone, isSome, optionConversion } from "ts-utility-kit/option";
const token = optionConversion(process.env.API_TOKEN);
if (isSome(token)) {
console.log(token.value);
}
const fallback = isNone(token) ? "guest" : token.value;
const fixed = createSome("ready");
const empty = createNone<string>();Included exports:
Option<T>createSome(value)createNone()isSome(option)isNone(option)optionConversion(value)
Use this when you want functions to return Result<T, E> instead of throwing directly.
import { UNIT, checkPromiseReturn, createErr, createOk, isErr, isOk } from "ts-utility-kit/result";
const result = await checkPromiseReturn({
fn: async () => fetchUser(),
err: (error) => createErr(error),
});
if (isOk(result)) {
console.log(result.value);
}
if (isErr(result)) {
console.error(result.err);
}
const done = createOk(UNIT);Included exports:
Result<T, E>createOk(value)createErr(error)isOk(result)isErr(result)UNITandUnitcheckResultReturn({ fn, err, finalFn? })checkResultVoid({ fn, err, finalFn? })checkPromiseReturn({ fn, err, finalFn? })checkPromiseVoid({ fn, err, finalFn? })
Use this when you only need shared TypeScript utility types.
import type { Dict, Without } from "ts-utility-kit/types";
interface User {
id: string;
name: string;
password: string;
}
type PublicUser = Without<User, "password">;
type UserMap = Dict<PublicUser>;Included exports:
Dict<T>Without<T, K>
The built files are committed to the release branch, so the package can be installed directly from this GitHub repository without running build scripts.