A lightweight, type-safe TypeScript library for generating prefixed IDs with customizable options. Perfect for creating consistent, identifiable IDs across your application (e.g., user_abc123xyz
, order_def456uvw
).
- 🔒 Type-safe - Full TypeScript support with proper type inference
- 🎯 Prefixed IDs - Generate IDs with consistent prefixes
- ⚙️ Highly Customizable - Configure separators, length, and character sets
- 📦 Lightweight - Minimal dependencies (only
nanoid
) - 🔍 Zod Integration - Built-in Zod validation schemas (optional)
- ✅ Valibot Integration - Built-in Valibot validation schemas (optional)
- 🧪 Well Tested - Comprehensive test coverage
- 📚 Modern ESM - ES modules with CommonJS support
npm install typed-id
# or
yarn add typed-id
# or
pnpm add typed-id
# or
bun add typed-id
For Zod validation support:
npm install zod
For Valibot validation support:
npm install valibot
import { IdHelper } from "typed-id";
// Create an ID helper for users
const userIdHelper = new IdHelper("user");
const userId = userIdHelper.generate();
console.log(userId); // "user_abc123xyz0" (example output)
const orderIdHelper = new IdHelper("order", {
separator: "::", // Custom separator
length: 12, // Custom ID length
customAlphabets: "ABCDEF123456789", // Custom character set
});
const orderId = orderIdHelper.generate();
console.log(orderId); // "order::A1B2C3D4E5F6" (example output)
import { IdHelper } from "typed-id";
// Different ID types for your application
const userIds = new IdHelper("user");
const postIds = new IdHelper("post", { separator: "-", length: 8 });
const sessionIds = new IdHelper("session", {
separator: "_",
length: 16,
customAlphabets: "0123456789abcdef", // Hex only
});
// Generate IDs
const user = userIds.generate(); // "user_abc123xyz0"
const post = postIds.generate(); // "post-ab12cd34"
const session = sessionIds.generate(); // "session_a1b2c3d4e5f67890"
You can use the InferId
utility type to extract the generated ID type from your IdHelper
instances:
import { IdHelper, type InferId } from "typed-id";
// Create ID helper instances
const userIdHelper = new IdHelper("user");
const orderIdHelper = new IdHelper("order", { separator: "::" });
// Infer the types that would be generated
type UserId = InferId<typeof userIdHelper>; // "user_${string}"
type OrderId = InferId<typeof orderIdHelper>; // "order::${string}"
// Use the inferred types in your application
function processUser(id: UserId) {
// id is guaranteed to be a user ID with the correct format
console.log(`Processing user: ${id}`);
}
// This will work
const userId = userIdHelper.generate();
processUser(userId); // ✅ Type-safe
// This would cause a TypeScript error
const orderId = orderIdHelper.generate();
// processUser(orderId); // ❌ Type error: Argument of type 'OrderId' is not assignable to parameter of type 'UserId'
If you're using Zod for validation, typed-id provides built-in schema creators:
import { IdHelper } from "typed-id";
import { createZodIdSchema } from "typed-id/validators/zod";
const userIdHelper = new IdHelper("user");
const userIdSchema = createZodIdSchema(userIdHelper);
// Validate IDs
const validId = userIdHelper.generate();
console.log(userIdSchema.safeParse(validId).success); // true
console.log(userIdSchema.safeParse("invalid_id").success); // false
// Use in your Zod schemas
import { z } from "zod";
const userSchema = z.object({
id: userIdSchema,
name: z.string(),
email: z.string().email(),
});
If you're using Valibot for validation, typed-id provides built-in schema creators:
import { IdHelper } from "typed-id";
import { createValibotIdSchema } from "typed-id/validators/validbot";
import { safeParse } from "valibot";
const userIdHelper = new IdHelper("user");
const userIdSchema = createValibotIdSchema(userIdHelper);
// Validate IDs
const validId = userIdHelper.generate();
console.log(safeParse(userIdSchema, validId).success); // true
console.log(safeParse(userIdSchema, "invalid_id").success); // false
// Use in your Valibot schemas
import { object, string, email } from "valibot";
const userSchema = object({
id: userIdSchema,
name: string(),
email: string([email()]),
});
Option | Type | Default | Description |
---|---|---|---|
separator |
string |
"_" |
Character(s) between prefix and ID |
length |
number |
10 |
Length of the generated ID part |
customAlphabets |
string |
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
Characters to use in ID generation |
new IdHelper<P extends string, S extends string | undefined>(
prefix: P,
options?: Partial<Options<S>>
)
Generates a new ID with the configured prefix and options.
Returns: A type-safe string in the format ${prefix}${separator}${id}
type Options<S extends string | undefined = undefined> = {
length: number;
separator: S;
customAlphabets: string;
};
type GeneratedID<P extends string, S extends string> = `${P}${S}${string}`;
A utility type that infers the generated ID type from an IdHelper
instance type. This is useful for type-level programming and ensuring type consistency across your application.
type InferId<T extends IdHelper<string, string>> = T extends IdHelper<
infer P,
infer S
>
? GeneratedId<P, SeparatorOrDefault<S>>
: never;
Usage Example:
import { IdHelper, InferId } from "typed-id";
// Create ID helper instances
const userIdHelper = new IdHelper("user");
const orderIdHelper = new IdHelper("order", { separator: "::" });
// Infer the types that would be generated
type UserId = InferId<typeof userIdHelper>; // "user_${string}"
type OrderId = InferId<typeof orderIdHelper>; // "order::${string}"
// Use the inferred types in your application
function processUser(id: UserId) {
// id is guaranteed to be a user ID with the correct format
console.log(`Processing user: ${id}`);
}
// This will work
const userId = userIdHelper.generate();
processUser(userId); // ✅ Type-safe
// This would cause a TypeScript error
const orderId = orderIdHelper.generate();
// processUser(orderId); // ❌ Type error: Argument of type 'OrderId' is not assignable to parameter of type 'UserId'
- Node.js 18+ or Bun
- TypeScript 5+
# Clone the repository
git clone https://github.com/max-programming/typed-id.git
cd typed-id
# Install dependencies
bun install
# Run tests
bun test
# Run tests in watch mode
bun test:ui
# Build the package
bun run build
# Type checking
tsc --noEmit
This library has comprehensive test coverage including:
- ID generation with default options
- ID generation with custom options
- Zod validation schemas
- Valibot validation schemas
- Type safety verification
Run tests:
bun test
Contributions are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with nanoid for secure, URL-friendly unique ID generation
- Inspired by the need for type-safe, prefixed identifiers in modern applications
- Thanks to all contributors who help improve this library
If you have any questions or issues, please:
- Check the existing issues
- Create a new issue with a clear description
- Include code examples and expected vs actual behavior
Made with ❤️ and TypeScript