Skip to main content
import type { StoreFieldDefinition, StoreSchema, StoresConfig, InferDocument } from '@collab-kit/utils';

StoreFieldDefinition

Defines a single field in a store schema.
interface StoreFieldDefinition {
  type: 'string' | 'number' | 'boolean';
  required?: boolean;
  default?: string | number | boolean;
}

StoreSchema

A record of field names to their definitions.
type StoreSchema = Record<string, StoreFieldDefinition>;

StoresConfig

A record of store names to their schemas. Passed to the client constructor.
type StoresConfig = Record<string, StoreSchema>;

InferDocument

Infers a TypeScript type from a store schema. Required fields become mandatory keys; optional fields become optional keys.
type InferDocument<S extends StoreSchema> = {
  [K in keyof S as S[K] extends { required: true } ? K : never]: FieldTypeMap[S[K]['type']];
} & {
  [K in keyof S as S[K] extends { required: true } ? never : K]?: FieldTypeMap[S[K]['type']];
};
Example:
const schema = {
  theme: { type: 'string' as const, required: true as const },
  fontSize: { type: 'number' as const, required: true as const },
  notifications: { type: 'boolean' as const },
};

// InferDocument<typeof schema> resolves to:
// { theme: string; fontSize: number; notifications?: boolean }