Welcome to the TypeScript Utility Library, a collection of reusable, type-safe, and elegant utilities designed to streamline your TypeScript projects. Whether you're handling deep object manipulations, managing asynchronous operations, or ensuring robust type safety, this library has you covered. 🌟
-
Deep Object Manipulations
Easily clone or freeze deeply nested objects with utilities likedeepCloneanddeepFreeze. -
Type-Safe Error Handling
Use theResulttype to handle errors gracefully and ensure predictable outcomes. -
Asynchronous Operation Helpers
Retry failed promises withretryor add timeouts withwithTimeoutfor better async flow control. -
Advanced Type Utilities
Simplify type management withDeepPartialand robust type guards likeassertNever. -
Typed Event Emitters
Enjoy strongly-typed event handling with theTypedEventEmitter.
To include this utility library in your project, simply run:
npm install my-ts-utility-libraryor
yarn add my-ts-utility-libraryimport { deepClone } from "my-ts-utility-library";
const original = { name: "Alice", details: { age: 25 } };
const cloned = deepClone(original);
console.log(cloned); // { name: "Alice", details: { age: 25 } }import { Result, ok, fail } from "my-ts-utility-library";
function divide(a: number, b: number): Result<number, string> {
if (b === 0) return fail("Division by zero");
return ok(a / b);
}
const result = divide(10, 2);
if (result.ok) {
console.log("Result:", result.value);
} else {
console.error("Error:", result.error);
}import { retry } from "my-ts-utility-library";
const fetchData = async () => {
// Simulate API call
if (Math.random() < 0.8) throw new Error("Random failure");
return "Success!";
};
retry(fetchData, 3, 1000)
.then(console.log)
.catch(console.error);import { TypedEventEmitter } from "my-ts-utility-library";
interface MyEvents {
data: string;
error: Error;
}
const emitter = new TypedEventEmitter<MyEvents>();
emitter.on("data", (data) => console.log("Data received:", data));
emitter.emit("data", "Hello, World!");import { withTimeout } from "my-ts-utility-library";
const fetchData = async () => {
await new Promise((r) => setTimeout(r, 3000)); // Simulate delay
return "Data fetched successfully!";
};
withTimeout(fetchData(), 2000)
.then(console.log)
.catch(console.error); // Will throw "Timeout exceeded"Deeply clones an object.
Parameters:
obj- The object to clone.
Returns:
The deeply cloned object.
A type-safe way to handle success and failure outcomes.
Example:
ok(value: T): Result<T>- Represents a successful result.fail(error: E): Result<never, E>- Represents a failure result.
Retries a failing asynchronous function.
Parameters:
fn- The function to retry.retries- Number of retry attempts (default: 3).delay- Delay between retries in milliseconds (default: 1000).
A strongly-typed event emitter.
Methods:
on<K extends keyof Events>(event: K, listener: (data: Events[K]) => void): voidemit<K extends keyof Events>(event: K, data: Events[K]): void
We welcome contributions! Here's how you can get involved:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Submit a pull request with detailed information.
Feel free to open an issue or start a discussion in the repository. We'd love to hear from you!
Special thanks to all contributors who make this project awesome. ❤️
This project is licensed under the MIT License.