UNPKG

typed-id

Version:

A lightweight, type-safe TypeScript library for generating prefixed IDs with customizable options

23 lines (20 loc) 980 B
type Options<S extends string | undefined = undefined> = { length: number; separator: S; customAlphabets: string; }; type GeneratedId<P extends string, S extends string> = `${P}${S}${string}`; type SeparatorOrDefault<S> = S extends string ? S : typeof IdHelper.DEFAULT_SEPARATOR; declare class IdHelper<P extends string, S extends string | undefined = undefined> { prefix: P; options: Partial<Options<S>>; static DEFAULT_SEPARATOR: "_"; static DEFAULT_LENGTH: 10; static DEFAULT_ALPHABETS: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; constructor(prefix: P, options?: Partial<Options<S>>); private get optionsOrDefaults(); get regex(): RegExp; generate(): GeneratedId<P, SeparatorOrDefault<S>>; } type InferId<T extends IdHelper<string, string>> = T extends IdHelper<infer P, infer S> ? GeneratedId<P, SeparatorOrDefault<S>> : never; export { IdHelper as I, type SeparatorOrDefault as S, type InferId as a };