Skip to content

codeismyid/typelab

Repository files navigation

typelab

License NPM Latest NPM Downloads

Github CI CodeQL

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

    const value1: Primitive = ""; // valid
    const value2: Primitive = {}; // invalid
  • Nullable

    const example: Nullable<string> = null; // valid
    const example2: Nullable<string> = undefined; // invalid
  • Undefinable

    const example: Undefinable<string> = undefined; // valid
    const example2: Undefinable<string> = null; // invalid
Assertions
  • IsExtends

    type Extends = IsExtends<'', string>; // true
    type NotExtends = IsExtends<string, ''>; // false
  • IsEqual

    type Equal = IsEqual<string, string>; // true
    type NotEqual = IsEqual<string, ''>; // false
  • IsAny

    type Valid = IsAny<any>; // true
    type Invalid = IsAny<string>; // false
Conditions
  • IfExtends

    type Yes = IfExtends<'', string, 'yes', 'no'>; // 'yes'
    type No = IfExtends<string, '', 'yes', 'no'>; // 'no'
    type YesOrNo = IfExtends<string | number, string, 'yes', 'no'>; // 'yes' | 'no'
  • IfEqual

    type Yes = IfEqual<string, string, 'yes', 'no'>; // 'yes'
    type No = IfEqual<string, number, 'yes', 'no'>; // 'no'
  • IfAny

    type Yes = IfAny<any, 'yes', 'no'>; // 'yes'
    type No = IfAny<string, 'yes', 'no'>; // 'no'
Utils
  • FunctionCallbackify

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

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

    // { a: string; b: string; c: boolean }
    type Assign1 = ObjectAssign<{ a: string; b: number }, { b: string; c: boolean }>;
    
    // { a: string; 0: string }
    type Assign2 = ObjectAssign<{ a: string }, [string]>;
    
    // { [x: number]: string, a: string }
    type Assign3 = ObjectAssign<{ a: string }, string[]>;
    
    // [number, number]
    type Assign4 = ObjectAssign<[string], [number, number]>;
    
    // (string | number)[]
    type Assign5 = ObjectAssign<string[], [number, number]>;
    
    // (string | number)[]
    type Assign6 = ObjectAssign<string[], number[]>;
    
    // [number, string]
    type Assign7 = ObjectAssign<[string], { 0: number; 1: string }>;
    
    // (string | number)[]
    type Assign8 = ObjectAssign<[number], 'str'>;
    
    // { [x: number]: string, a: string }
    type Assign9 = 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

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors