NewSortable IDs are here
prefID
Menu

Types

The public types prefID exports, so you can annotate your own functions and stores.

PrefixedId

The core type. PrefixedId<P> is a template literal type describing any string that starts with ${P}_. It takes an optional second parameter, S, for the separator — it defaults to "_", so PrefixedId<"user"> keeps its usual meaning.

ts
type PrefixedId<
  P extends string = string,
  S extends string = "_", // the separator
> = `${P}${S}${string}`;
ts
import type { PrefixedId } from "prefid";

type UserId = PrefixedId<"user">;
//   = `user_${string}`

function getUser(id: UserId) { /* ... */ }

getUser("user_abc"); // ✅
getUser("order_abc"); // ❌ not assignable to `user_${string}`

Custom separators are type-sound

When you build a generator with a non-default separator, that separator is carried through to the value's type — so a "-"-separated ID is typed `${P}-${string}` and won't be mistaken for an underscore one. The same holds for createSortableId and isId(value, prefix, separator).

ts
import { createId } from "prefid";

// The literal separator flows into the value's type:
const gen = createId({ separator: "-" });
const uid = gen("user");
//    ^? `user-${string}`   (a PrefixedId<"user", "-">)

const wrong: `user_${string}` = uid; // ❌ "-" ids aren't "_" ids

IdOptions & IdGenerator

IdOptions is the options object accepted by createId, and IdGenerator<S> is the type of the generator it returns — generic over the separator S, which defaults to "_".

ts
import type { IdOptions, IdGenerator } from "prefid";

const options: IdOptions = { size: 16, separator: "_" };

// IdGenerator is generic over the separator (defaults to "_"):
const gen: IdGenerator = createId(options);
const dashed: IdGenerator<"-"> = createId({ separator: "-" });

EnsureUniqueOptions

The options object accepted by ensureUnique — currently just maxAttempts.