The Element type is the foundation for all types in Stoneforge. It provides a unified identity system, timestamp tracking, tagging, and metadata capabilities.
Stoneforge uses TypeScript branded types for type safety:
// Branded type for Element IDsdeclare const ElementIdBrand: unique symbol;export type ElementId = string & { readonly [ElementIdBrand]: typeof ElementIdBrand};// Branded type for Entity IDsdeclare const EntityIdBrand: unique symbol;export type EntityId = string & { readonly [EntityIdBrand]: typeof EntityIdBrand};// Timestamp type - ISO 8601 formatted stringexport type Timestamp = string;
Only alphanumeric, hyphen, underscore, colon allowed
No leading or trailing whitespace
No duplicates
// Validate a single tagvalidateTag(tag: unknown): string// Validate tags arrayvalidateTags(tags: unknown): string[]// Type guardisValidTag(tag: unknown): tag is string
// Validate ISO 8601 timestampvalidateTimestamp(value: unknown, field: string): Timestamp// Type guardisValidTimestamp(value: unknown): value is Timestamp// Create new timestampcreateTimestamp(): Timestamp// Parse timestamp to DateparseTimestamp(timestamp: Timestamp): Date
// Check if value is valid ElementisElement(value: unknown): value is Element// Check if value is valid ElementTypeisValidElementType(value: unknown): value is ElementType// Comprehensive validation with detailed errorsvalidateElement(value: unknown): ElementvalidateElementType(value: unknown): ElementType
Show Example Usage
import { isElement, validateElement } from '@stoneforge/core';// Type guard approachif (isElement(data)) { console.log(data.id);}// Validation approach (throws on error)try { const element = validateElement(data); console.log(element.id);} catch (error) { console.error('Invalid element:', error.message);}
// Normalize tags (dedupe and sort)normalizeTags(tags: string[]): string[]// Add tag to element (validates and dedupes)addTag(element: Element, tag: string): string[]// Remove tag from elementremoveTag(element: Element, tag: string): string[]