Skip to content

FusedKush/typescript-toolkit

Repository files navigation

TypeScript Toolkit

A collection of various TypeScript tools, tricks, and utilities that can be used in a wide range of projects and programs.

Using the TypeScript Toolkit

The TypeScript Toolkit can be used in two different ways:

  1. As a collection of TypeScript/JavaScript code snippets.
  2. As an NPM Package.

...as a Collection of Code Snippets

The primary intention of the TypeScript Toolkit is to serve as a collection of versatile and high-quality TypeScript and JavaScript code snippets. Each tool in the toolkit, such as arrays/arrayify or types/baseTypes, contains two relevant folders:

  • /ts/, which contains the TypeScript code snippets.
  • /js/, which contains the JavaScript code snippets.

The code snippets themselves are then typically broken down into two or more files, including, but not limited to:

  • index.ts or index.js, which contains the standard code as it would be used along with any dependencies.
  • no-deps.ts or no-deps.js, which contains the code without any (external) dependencies.

Each code snippet can be copied-and-pasted directly into the desired codebase. Ensure the documentation blocks and source information is copied as well, which will make it easier to check for future updates or report issues if necessary.

...as an NPM Package

The TypeScript Toolkit can also be used as an NPM Package. Each tool in the toolkit, such as arrays/arrayify or types/baseTypes, contains two relevant files and folders:

  • /module/, which contains the TypeScript module code.
  • /index.ts, which simply re-exports the contents of /module/index.ts.

To install the package as a dependency of your project:

npm install typescript-toolkit

You can then import any of the tools in the toolkit just as you would any other NPM Package:

// These imports are all equivalent
import arrayify from "typescript-toolkit/arrays/arrayify";
import { UnionToIntersection } from "typescript-toolkit/types/unionToIntersection";
import { IsAny } from "typescript-toolkit/types";

type IsAnyType = IsAny<any>;
type IntersectionObject = UnionToIntersection<{ foo: string; } | { bar: number; }>;
const arrayifiedFoobar = arrayify("foobar");

You can also import the namespaces themselves into your project:

// These imports are all equivalent
import { arrays, types } from "typescript-toolkit";
import arrays from "typescript-toolkit/arrays";
import * as types from "typescript-toolkit/types";

type IsAnyType = types.IsAny<any>;
type IntersectionObject = types.UnionToIntersection<{ foo: string; } | { bar: number; }>;
const arrayifiedFoobar = arrays.arrayify("foobar");

For JavaScript projects, you can use import() types or @import tags to virtually import type-based tools and namespaces:

/**
 * @typedef {import("typescript-toolkit/types").IsAny<any>} IsAnyType
 */
/**
 * @typedef {import("typescript-toolkit").arrays.ArrayifyType<"foobar">} ArrayifiedFoobar
 */

/**
 * @import UnionToIntersection from "typescript-toolkit/types/unionToIntersection"
 * @import { ArrayifyType } from "typescript-toolkit/arrays"
 */
/**
 * @typedef {UnionToIntersection<{ foo: string; } | { bar: number; }>} IntersectionObject
 */
/**
 * @typedef {ArrayifyType<"foobar">} ArrayifiedFoobar
 */

/**
 * @import arrays from "typescript-toolkit/arrays"
 * @import * as types from "typescript-toolkit/types"
 * @import { arrays, types } from "typescript-toolkit"
 */
/**
 * @typedef {types.IsAny<any>} IsAnyType
 */
/**
 * @typedef {arrays.ArrayifyType<"foobar">} ArrayifiedFoobar
 */

Important

While simply adding the TypeScript Toolkit as a dependency of your project makes importing, updating, and working with the various tools in the toolkit effortless, it can also add considerable size and unnecessary bloat if only a couple of tools are being used and those tools are unlikely to be updated in the future.

Unless a large number of tools from the toolkit or up-to-date code snippets are required for your project, it is strongly recommended to just use the toolkit as collection of TypeScript/JavaScript code snippets instead.

More Information

More information about the individual namespaces and tools in the TypeScript Toolkit can be found in their respective README.md files.

API Documentation for the TypeScript Toolkit is available on the Repository Wiki.

For licensing information, see LICENSE.

For information about filing issues, submitting suggestions, and reporting security vulnerabilities, see CONTRIBUTING.md and SECURITY.md.

For information about contributing to the TypeScript Toolkit Project, see CONTRIBUTING.md;

About

A collection of various TypeScript tools, tricks, and utilities that can be used in a wide range of projects and programs.

Topics

Resources

License

Security policy

Stars

Watchers

Forks