FuncType - v0.8.85
    Preparing search index...

    Function ValidatedBrand

    • Create a validated brand with runtime validation

      Type Parameters

      • K extends string
      • T

      Parameters

      • brand: K
      • validate: (value: T) => boolean

      Returns ValidatedBrand<K, T>

      const Email = ValidatedBrand("Email", (s: string) => /^[^@]+@[^@]+\.[^@]+$/.test(s))
      const email = Email.of("user@example.com") // Some(Brand<"Email", string>)
      // With Either for error messages
      const Port = ValidatedBrand("Port", (n: number) => n >= 1 && n <= 65535)
      const result = Port.from(8080) // Right(Brand<"Port", number>)
      const error = Port.from(70000) // Left("Invalid Port: validation failed")
      // Type guard usage
      const value: unknown = "test@example.com"
      if (Email.is(value)) {
      // value is Brand<"Email", string>
      }