TS Utility: Enhance Your TypeScript Workflow

Streamlining common development tasks for modern applications.

Introduction to TS Utility

In the rapidly evolving landscape of web development, TypeScript has become a cornerstone for building robust and scalable applications. To further optimize the development experience, we introduce TS Utility, a collection of powerful, easy-to-use functions designed to simplify common TypeScript tasks. Whether you're working with complex data structures, managing asynchronous operations, or enhancing type safety, TS Utility provides elegant solutions that integrate seamlessly into your projects.

Our goal is to reduce boilerplate code, improve readability, and boost developer productivity, allowing you to focus on the core logic of your application.

Key Features

Example: Type Guard for Discriminated Unions

Safely narrowing down types in discriminated unions is a common pattern. TS Utility makes this a breeze:


import { isType } from 'ts-utility'; // Assuming installation

interface Circle {
  kind: 'circle';
  radius: number;
}

interface Square {
  kind: 'square';
  sideLength: number;
}

type Shape = Circle | Square;

function getArea(shape: Shape): number {
  if (isType<Circle>(shape, 'kind', 'circle')) {
    // TypeScript knows shape is Circle here
    return Math.PI * shape.radius ** 2;
  } else {
    // TypeScript knows shape is Square here
    return shape.sideLength ** 2;
  }
}

const myCircle: Circle = { kind: 'circle', radius: 10 };
console.log(`Circle area: ${getArea(myCircle)}`);
            

The isType helper function provides compile-time type checking, reducing the need for verbose type predicate functions.

Example: Deep Object Merging

Combine objects recursively, ensuring nested properties are merged correctly.


import { deepMerge } from 'ts-utility'; // Assuming installation

interface UserPreferences {
  theme: {
    darkMode: boolean;
    fontSize: number;
  };
  notifications: {
    email: boolean;
    sms: boolean;
  };
}

const defaultPrefs: UserPreferences = {
  theme: { darkMode: false, fontSize: 14 },
  notifications: { email: true, sms: false }
};

const userPrefs: Partial<UserPreferences> = {
  theme: { darkMode: true },
  notifications: { sms: true }
};

const mergedPrefs = deepMerge(defaultPrefs, userPrefs);

console.log(JSON.stringify(mergedPrefs, null, 2));
/*
Output:
{
  "theme": {
    "darkMode": true,
    "fontSize": 14
  },
  "notifications": {
    "email": true,
    "sms": true
  }
}
*/
            

deepMerge ensures that even deeply nested properties are handled appropriately without overwriting entire sub-objects unless intended.

Getting Started

Integrating TS Utility into your project is straightforward. You can install it via npm or yarn:


npm install ts-utility
# or
yarn add ts-utility
            

Once installed, you can import and use its functions directly in your TypeScript files. Explore the documentation for a comprehensive list of available utilities and detailed usage examples.

Explore Documentation View More Examples