TypeScript Advanced Types: My Daily Essentials Now on NPM

ggondim

Gustavo Gondim

Posted on November 14, 2024

TypeScript Advanced Types: My Daily Essentials Now on NPM

The ts-advanced-types library is a collection of utility types designed to simplify and enhance TypeScript development. I started it four years ago to address common challenges such as excluding specific properties from objects, enforcing mutually exclusive type options, and defining tree-like data models, making your code more expressive and maintainable.

Built to streamline everyday TypeScript tasks, this library consolidates frequently used advanced types into a reusable package. By sharing these tools with the community, my goal is to help developers write cleaner, more reliable code and save time across projects.

Installation and setup

Setting up the ts-advanced-types library is straightforward. Since the project is not transpiled, it directly exposes all types and utilities through its index.ts file. This ensures a lightweight and hassle-free integration into your TypeScript project.

Steps to install:

1 Add the library to your project via npm or yarn:

npm install ts-advanced-types
# or
yarn add ts-advanced-types
Enter fullscreen mode Exit fullscreen mode

2 Import the required types or utilities directly from the package:

import { TypeXOR, Without } from 'ts-advanced-types';
Enter fullscreen mode Exit fullscreen mode

No additional configuration is needed—you’re ready to start using the library. This simplicity allows you to focus entirely on leveraging the provided tools to enhance your TypeScript workflows.

What's inside

The library includes a wide range of utility types and helpers that solve common TypeScript challenges. From enforcing stricter object constraints to creating mutually exclusive type options, each utility is designed to address specific use cases while improving code clarity and maintainability. These types are all ready to use and can seamlessly integrate into your existing projects, saving you time and effort

Utility types

  • Without<T, U> Remove all properties from T that are assignable to U
  • TypeXOR<T, U> XOR of two types

Basic types

  • Falsy JavaScript falsy types
  • PrimitiveValidIndexSignature JavaScript primitive types accepted as index signatures
  • Primitive JavaScript primitive non-falsy types
  • Complex JavaScript non-falsy types
  • FalsyOrLiteral JavaScript primitive types, including falsy values
  • Document<T = Complex> An object made of string keys and non-falsy values. To add new types to values, use the T type parameter.
  • JsonOrString A JSON, as a string or as a parsed object or array

Advanced types/classes

  • TreeItem<T> A generic tree
  • EmptyConstructorOf<T> A type that implements a constructor without arguments
  • ClonableType<T> A type that is clonable: it can be instantiated with a partial object

Utility functions

  • isFalsyOrSpaces(value) Check if a value is falsy or a string with only spaces, ignoring number 0
  • withoutProps(obj, ...props) Clones an object, optionally removing a list of properties
  • equals(a, b) Checks if two objects are equal using the equals method or strict equality
  • getMethods(obj) List all methods of an object and its prototypes

Contribute and share your feedback

The library is an evolving project, and contributions from the developer community are always welcome. If you have ideas for new utility types, improvements, or discover any issues, feel free to open a pull request or report an issue on the GitHub repository.

Your feedback is invaluable in shaping the library to better serve TypeScript developers. If you’ve used ts-advanced-types in your projects, I’d love to hear about your experience! Leave a comment on this post or share your thoughts directly on GitHub. Together, we can make TypeScript development even more efficient and enjoyable.

💖 💪 🙅 🚩
ggondim
Gustavo Gondim

Posted on November 14, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How Changsets reads config.json internally
javascript How Changsets reads config.json internally

November 22, 2024

Release 0.3 #1
typescript Release 0.3 #1

November 19, 2024

arrayToDict function in tRPC source code
javascript arrayToDict function in tRPC source code

November 2, 2024