Skip to content

Repository files navigation

typelab

LicenseNPM LatestNPM Downloads

GithubCICodeQL

A lab of reusable utility types for everyday TypeScript code.

📝 Table of Contents

🤔 About

This package offers a suite of versatile utility types that simplify common tasks, improve type safety, and boost productivity.

  • Easy to use
  • Zero third party dependencies
  • Type level only
  • No more copy paste "type definitions" between projects

🔌 Installation

# NPM
npm install --save-dev typelab
# BUN
bun add -d typelab

📔 Docs

Showcases

Here is an example for some types from each categories.

Aliases
  • Primitive

    constvalue1: Primitive="";// validconstvalue2: Primitive={};// invalid
  • Nullable

    constexample: Nullable<string>=null;// validconstexample2: Nullable<string>=undefined;// invalid
  • Undefinable

    constexample: Undefinable<string>=undefined;// validconstexample2: Undefinable<string>=null;// invalid
Assertions
  • IsExtends

    typeExtends=IsExtends<'',string>;// truetypeNotExtends=IsExtends<string,''>;// false
  • IsEqual

    typeEqual=IsEqual<string,string>;// truetypeNotEqual=IsEqual<string,''>;// false
  • IsAny

    typeValid=IsAny<any>;// truetypeInvalid=IsAny<string>;// false
Conditions
  • IfExtends

    typeYes=IfExtends<'',string,'yes','no'>;// 'yes'typeNo=IfExtends<string,'','yes','no'>;// 'no'typeYesOrNo=IfExtends<string|number,string,'yes','no'>;// 'yes' | 'no'
  • IfEqual

    typeYes=IfEqual<string,string,'yes','no'>;// 'yes'typeNo=IfEqual<string,number,'yes','no'>;// 'no'
  • IfAny

    typeYes=IfAny<any,'yes','no'>;// 'yes'typeNo=IfAny<string,'yes','no'>;// 'no'
Utils
  • FunctionCallbackify

    // (a: string, callback: (error: unknown, result: string) => void) => voidtypeCallbackified=FunctionCallbackify<(a: string)=>Promise<string>>;
  • FunctionPromisify

    // Callback-style function:// (a: string) => Promise<string>typePromisified=FunctionPromisify<(a: string,callback: (error: Error,result: string)=>void)=>void>;// Regular function:// (a: string) => Promise<string>typePromisifiedFn=FunctionPromisify<(a: string)=>string>;
  • ObjectAssign

    // { a: string; b: string; c: boolean }typeAssign1=ObjectAssign<{a: string;b: number},{b: string;c: boolean}>;// { a: string; 0: string }typeAssign2=ObjectAssign<{a: string},[string]>;// { [x: number]: string, a: string }typeAssign3=ObjectAssign<{a: string},string[]>;// [number, number]typeAssign4=ObjectAssign<[string],[number,number]>;// (string | number)[]typeAssign5=ObjectAssign<string[],[number,number]>;// (string | number)[]typeAssign6=ObjectAssign<string[],number[]>;// [number, string]typeAssign7=ObjectAssign<[string],{0: number;1: string}>;// (string | number)[]typeAssign8=ObjectAssign<[number],'str'>;// { [x: number]: string, a: string }typeAssign9=ObjectAssign<{a: string},'str'>;

All Available Types

A complete reference for all available reusable types.

Aliases
Assertions
Conditions
Utils

⛏️ Built Using

  • Typescript
    Strongly typed programming language that builds on JavaScript.
  • Bun
    All-in-one JavaScript runtime & toolkit designed for speed, complete with a bundler, test runner, and Node.js-compatible package manager.

📄 License

The code in this project is released under the MIT License.

About

A lab of reusable utility types for everyday TypeScript code.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages