Typedash Utility Functions
Typedash is a modern, type-safe collection of utility functions designed for TypeScript environments, offering a robust alternative to libraries like Lodash with a strong focus on simplicity, tree-shaking, and precise type inference. It currently stands at version 3.3.3 and maintains a fairly active release cadence with frequent minor and patch updates, as evidenced by recent changelog entries. Key differentiators include its complete type-safety, ensuring type-level transformations for string casing functions and precise key/value types for object utilities. The library boasts zero runtime dependencies, making it highly tree-shakeable and performant. It operates on the principle of "trust the compiler" by leveraging TypeScript's robust type system for correctness rather than relying on extensive runtime checks. Typedash provides over 60 utility functions categorized for Array, Object, String, Type Guards, Function, and Math operations.
Common errors
-
TypeError: (0, typedash_1.pick) is not a function
cause This error typically indicates incorrect CommonJS `require` syntax, bundler misconfiguration, or an attempt to import a module primarily designed for ESM using CJS patterns.fixEnsure you are using `import { functionName } from 'typedash'` for named ESM imports in a compatible environment. If using CommonJS, individual function imports like `const pick = require('typedash/pick')` are often more reliable, as full CJS support for the main entry point may vary by version and build setup. -
Property 'xyz' does not exist on type 'ABC'.
cause Attempting to access a property that TypeScript's inferred type for the object does not include, often due to an incorrect type assertion or unexpected input. Typedash's strict typing will surface these issues.fixReview your object's type definition and ensure it accurately reflects all possible properties. Use type assertions (`as Type`) sparingly and only when you are absolutely certain of the type. For dynamic property access, consider using type guards or utility functions like `objectKeys` to safely narrow types. -
Argument of type 'string[]' is not assignable to parameter of type 'keyof T | readonly (keyof T)[]'.
cause Providing an array of generic `string` literals where Typedash expects an array of specific literal keys derived from the object's type, typically when using functions like `pick` or `omit`.fixEnsure the array of keys passed to functions like `pick` or `omit` is correctly typed as a `const` assertion (`['a', 'b'] as const`) or explicitly as a union of literal types (e.g., `const keys: Array<'a' | 'b'> = ['a', 'b'];`) so TypeScript can infer the precise subset of keys.
Warnings
- gotcha Typedash relies heavily on TypeScript's type system for correctness and performs minimal runtime validation. Unlike libraries such as Lodash, it generally does not include extensive runtime checks for input types or values, operating on the principle of 'trust the compiler'.
- breaking Typedash requires Node.js version 20.0.0 or higher. Older Node.js versions are not supported and may lead to runtime errors or unexpected behavior due to modern language features and module resolution.
- breaking Versions of Typedash prior to 3.2.3 may have experienced issues with CommonJS consumers, specifically regarding module resolution and incorrect builds, which could lead to `TypeError: ... is not a function` errors when using `require()`.
Install
-
npm install typedash -
yarn add typedash -
pnpm add typedash
Imports
- pick
const { pick } = require('typedash')import { pick } from 'typedash' - camelCase
import { camelCase } from 'typedash' - objectKeys
import objectKeys from 'typedash/objectKeys'
import { objectKeys } from 'typedash/objectKeys'
Quickstart
import { pick, objectKeys, camelCase, compact } from 'typedash';
interface User {
id: string;
firstName: string;
lastName: string;
email: string;
isAdmin?: boolean;
'user-role': string;
}
const userData: User = {
id: '123',
firstName: 'John',
lastName: 'Doe',
email: 'john.doe@example.com',
isAdmin: false,
'user-role': 'admin'
};
// Demonstrate pick for selecting properties with preserved types
const publicInfo = pick(userData, ['id', 'firstName', 'lastName']);
console.log('Public Info:', publicInfo);
// Expected type: { id: string; firstName: string; lastName: string; }
// Demonstrate objectKeys with precise type inference for array of keys
const keys = objectKeys(publicInfo);
console.log('Keys:', keys);
// Expected type: ("id" | "firstName" | "lastName")[]
// Demonstrate camelCase for string literal transformation
const userRoleKey = camelCase(userData['user-role']);
console.log('CamelCased Role Key:', userRoleKey);
// Expected type: "userRole"
// Demonstrate compact for array filtering, inferring non-falsy types
const values = [0, 1, false, '', 'hello', null, undefined];
const compacted = compact(values);
console.log('Compacted Array:', compacted);
// Expected type: (1 | "hello")[]