functype 0.8.85 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.md +108 -27
  2. package/dist/{Either-BlY4VB1r.d.ts → Either-BHep7I0d.d.ts} +5 -53
  3. package/dist/Serializable-BbKuhDDL.d.ts +78 -0
  4. package/dist/branded/index.d.ts +8 -7
  5. package/dist/branded/index.mjs +1 -1
  6. package/dist/chunk-BQJB6CCW.mjs +3 -0
  7. package/dist/chunk-BQJB6CCW.mjs.map +1 -0
  8. package/dist/chunk-GHBOC52G.mjs +43 -0
  9. package/dist/chunk-GHBOC52G.mjs.map +1 -0
  10. package/dist/chunk-R2TQJN3P.mjs +2 -0
  11. package/dist/chunk-R2TQJN3P.mjs.map +1 -0
  12. package/dist/either/index.d.ts +2 -2
  13. package/dist/either/index.mjs +1 -1
  14. package/dist/fpromise/index.d.ts +2 -2
  15. package/dist/fpromise/index.mjs +1 -1
  16. package/dist/index.d.ts +554 -147
  17. package/dist/index.mjs +1 -1
  18. package/dist/list/index.d.ts +2 -2
  19. package/dist/list/index.mjs +1 -1
  20. package/dist/map/index.d.ts +3 -3
  21. package/dist/map/index.mjs +1 -1
  22. package/dist/option/index.d.ts +2 -2
  23. package/dist/option/index.mjs +1 -1
  24. package/dist/set/index.d.ts +2 -2
  25. package/dist/set/index.mjs +1 -1
  26. package/dist/try/index.d.ts +2 -2
  27. package/dist/try/index.mjs +1 -1
  28. package/dist/tuple/index.d.ts +75 -2
  29. package/dist/tuple/index.mjs +1 -1
  30. package/package.json +13 -13
  31. package/readme/BUNDLE_OPTIMIZATION.md +74 -0
  32. package/readme/FPromise-Assessment.md +43 -0
  33. package/readme/HKT.md +110 -0
  34. package/readme/ROADMAP.md +113 -0
  35. package/readme/TASK-TODO.md +33 -0
  36. package/readme/TUPLE-EXAMPLES.md +76 -0
  37. package/readme/TaskMigration.md +129 -0
  38. package/readme/ai-guide.md +406 -0
  39. package/readme/examples.md +2093 -0
  40. package/readme/quick-reference.md +514 -0
  41. package/readme/task-error-handling.md +283 -0
  42. package/readme/tasks.md +203 -0
  43. package/readme/type-index.md +238 -0
  44. package/dist/Tuple-ZYh96cGE.d.ts +0 -41
  45. package/dist/Typeable-BBXrKPTY.d.ts +0 -14
  46. package/dist/chunk-B6FR572T.mjs +0 -2
  47. package/dist/chunk-B6FR572T.mjs.map +0 -1
  48. package/dist/chunk-JAPOUVPL.mjs +0 -42
  49. package/dist/chunk-JAPOUVPL.mjs.map +0 -1
  50. package/dist/chunk-TQJDL6YW.mjs +0 -2
  51. package/dist/chunk-TQJDL6YW.mjs.map +0 -1
package/dist/index.d.ts CHANGED
@@ -1,24 +1,16 @@
1
1
  import { Brand } from './branded/index.js';
2
2
  export { BrandedBoolean, BrandedNumber, BrandedString, ExtractBrand, Unbrand, createBrander, hasBrand, unbrand } from './branded/index.js';
3
- import { O as Option, E as Either, F as Foldable, L as List, a as FunctypeBase, b as Extractable, P as Pipe, T as Traversable, S as Serializable, M as Matchable } from './Either-BlY4VB1r.js';
4
- export { A as Applicative, s as AsyncMonad, C as Collection, q as CollectionOps, r as ContainerOps, u as Functor, j as Functype, k as FunctypeCollection, d as Left, l as MatchableUtils, v as Monad, N as None, n as OptionConstructor, R as Right, o as SerializationMethods, p as Set, m as Some, c as TestEither, g as TypeCheckLeft, f as TypeCheckRight, e as isLeft, i as isRight, t as tryCatch, h as tryCatchAsync } from './Either-BlY4VB1r.js';
5
- import { T as Type, a as Typeable } from './Typeable-BBXrKPTY.js';
6
- export { E as ExtractTag, b as TypeableParams, i as isTypeable } from './Typeable-BBXrKPTY.js';
3
+ import { O as Option, E as Either, L as List, F as FunctypeBase, a as Extractable, T as Traversable, M as Matchable } from './Either-BHep7I0d.js';
4
+ export { A as Applicative, q as AsyncMonad, C as Collection, o as CollectionOps, p as ContainerOps, r as Functor, j as Functype, k as FunctypeCollection, c as Left, l as MatchableUtils, s as Monad, N as None, m as OptionConstructor, R as Right, n as Set, S as Some, b as TestEither, f as TypeCheckLeft, e as TypeCheckRight, h as isExtractable, d as isLeft, i as isRight, t as tryCatch, g as tryCatchAsync } from './Either-BHep7I0d.js';
5
+ import { T as Type, a as Typeable, F as Foldable, P as Pipe, S as Serializable } from './Serializable-BbKuhDDL.js';
6
+ export { b as SerializationMethods } from './Serializable-BbKuhDDL.js';
7
7
  import { FPromise } from './fpromise/index.js';
8
8
  export { ErrorContext, FPromiseCompanion } from './fpromise/index.js';
9
9
  import { Try } from './try/index.js';
10
10
  export { TypeNames } from './try/index.js';
11
11
  export { Map, SafeTraversable } from './map/index.js';
12
- import { V as Valuable } from './Tuple-ZYh96cGE.js';
13
- export { T as Tuple, a as ValuableParams } from './Tuple-ZYh96cGE.js';
12
+ export { Tuple } from './tuple/index.js';
14
13
 
15
- /**
16
- * A brand with runtime validation
17
- * @example
18
- * const Age = ValidatedBrand("Age", (n: number) => n >= 0 && n <= 150)
19
- * const myAge = Age.of(25) // Option<Brand<"Age", number>>
20
- * const invalid = Age.of(-5) // None
21
- */
22
14
  type ValidatedBrand<K extends string, T> = {
23
15
  readonly brand: K;
24
16
  readonly validate: (value: T) => boolean;
@@ -145,6 +137,7 @@ declare function PatternString(brand: string, pattern: RegExp): ValidatedBrand<s
145
137
  */
146
138
  declare function Companion<ObjectF extends object, CompanionF extends object>(object: ObjectF, companion: CompanionF): ObjectF & CompanionF;
147
139
 
140
+ /** @internal */
148
141
  type LazyCondChain<T> = {
149
142
  when: (condition: () => boolean, value: () => T) => LazyCondChain<T>;
150
143
  elseWhen: (condition: () => boolean, value: () => T) => LazyCondChain<T>;
@@ -276,11 +269,35 @@ declare const Cond: (<T extends Type>() => Cond<T>) & {
276
269
  lazy: <T extends Type>() => LazyCondChain<T>;
277
270
  };
278
271
 
272
+ /**
273
+ * Type-level utilities for exhaustiveness checking
274
+ * @internal
275
+ */
276
+ type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
277
+ /** @internal */
278
+ type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;
279
+ /** @internal */
280
+ type RequireExhaustive<T, Cases> = IsUnion<T> extends true ? (keyof Cases extends T ? (T extends keyof Cases ? Cases : never) : never) : Cases;
281
+ /**
282
+ * Pattern types for nested matching
283
+ * @internal
284
+ */
285
+ type Pattern<T> = T | {
286
+ [K in keyof T]?: Pattern<T[K]>;
287
+ } | ((value: T) => boolean) | {
288
+ _: (value: T) => boolean;
289
+ };
290
+ /**
291
+ * Extract result from pattern
292
+ * @internal
293
+ */
294
+ type PatternResult<T, R> = R | ((matched: T) => R);
279
295
  /**
280
296
  * Pattern matching construct similar to Scala's match expressions.
281
- * Enforces exhaustive matching and returns values.
297
+ * Supports exhaustive matching, nested patterns, and guards.
282
298
  *
283
299
  * @example
300
+ * // Basic pattern matching
284
301
  * const result = Match(value)
285
302
  * .case(x => x > 100, "large")
286
303
  * .case(x => x > 50, "medium")
@@ -293,31 +310,57 @@ declare const Cond: (<T extends Type>() => Cond<T>) & {
293
310
  * .caseValue("success", "Completed!")
294
311
  * .caseValue("error", "Failed")
295
312
  * .default("Unknown")
313
+ *
314
+ * @example
315
+ * // Nested pattern matching
316
+ * const user = { name: "John", age: 30, role: "admin" }
317
+ * const msg = Match(user)
318
+ * .case({ role: "admin", age: n => n >= 18 }, "Adult admin")
319
+ * .case({ role: "user" }, u => `User: ${u.name}`)
320
+ * .default("Guest")
296
321
  */
297
322
  type Match<T extends Type, R extends Type> = {
298
323
  /**
299
- * Add a case with a predicate
324
+ * Match against a pattern (value, nested object, or predicate)
300
325
  */
301
- case: (predicate: (value: T) => boolean, result: R | ((value: T) => R)) => Match<T, R>;
326
+ case: (pattern: Pattern<T>, result: PatternResult<T, R>) => Match<T, R>;
302
327
  /**
303
- * Add a case that matches a specific value
328
+ * Add a case that matches a specific value (backward compatibility)
304
329
  */
305
330
  caseValue: (match: T, result: R | (() => R)) => Match<T, R>;
306
331
  /**
307
- * Add a case that matches multiple values
332
+ * Add a case that matches multiple values (backward compatibility)
308
333
  */
309
334
  caseValues: (matches: T[], result: R | (() => R)) => Match<T, R>;
310
335
  /**
311
- * Default case - required to get result
336
+ * Match with a guard function (alias for readability)
337
+ */
338
+ when: (guard: (value: T) => boolean, result: PatternResult<T, R>) => Match<T, R>;
339
+ /**
340
+ * Match multiple patterns (OR operation)
341
+ */
342
+ caseAny: (patterns: Pattern<T>[], result: PatternResult<T, R>) => Match<T, R>;
343
+ /**
344
+ * Default case - makes match non-exhaustive
345
+ */
346
+ default: (result: PatternResult<T, R>) => R;
347
+ /**
348
+ * Force exhaustive matching (compile-time check for union types)
349
+ */
350
+ exhaustive: () => R;
351
+ /**
352
+ * Get result if matched, throws if no match
312
353
  */
313
- default: (result: R | ((value: T) => R)) => R;
354
+ getOrThrow: (errorMessage?: string) => R;
314
355
  /**
315
- * Get result if a case matched, throws if no match
356
+ * Get result wrapped in Option
316
357
  */
317
- getOrThrow: () => R;
358
+ toOption: () => Option<R>;
318
359
  };
319
360
  /**
320
- * Pattern matching utility for type-safe conditional logic
361
+ * Pattern matching utility for type-safe conditional logic with exhaustiveness checking,
362
+ * nested patterns, and guard support
363
+ *
321
364
  * @example
322
365
  * // Basic pattern matching
323
366
  * const result = Match(value)
@@ -335,12 +378,24 @@ type Match<T extends Type, R extends Type> = {
335
378
  * })(color)
336
379
  *
337
380
  * @example
338
- * // Partial matching with default
339
- * const name = Match.partial<number, string>({
340
- * 1: "one",
341
- * 2: "two",
342
- * 3: "three"
343
- * }).withDefault(n => `number ${n}`)(value)
381
+ * // Nested pattern matching
382
+ * type User = { name: string; age: number; role: "admin" | "user" }
383
+ * const user: User = { name: "John", age: 30, role: "admin" }
384
+ *
385
+ * const message = Match<User, string>(user)
386
+ * .case({ role: "admin", age: (n) => n >= 18 }, "Adult admin")
387
+ * .case({ role: "user" }, u => `User: ${u.name}`)
388
+ * .default("Unknown")
389
+ *
390
+ * @example
391
+ * // Using exhaustive() method
392
+ * type Status = "idle" | "loading" | "success" | "error"
393
+ * const result = Match<Status, string>("success")
394
+ * .case("idle", "Waiting...")
395
+ * .case("loading", "Loading...")
396
+ * .case("success", "Done!")
397
+ * .case("error", "Failed!")
398
+ * .exhaustive()
344
399
  */
345
400
  declare const Match: (<T extends Type, R extends Type>(value: T) => Match<T, R>) & {
346
401
  /**
@@ -366,7 +421,7 @@ declare const Match: (<T extends Type, R extends Type>(value: T) => Match<T, R>)
366
421
  * const compute = ops("multiply").fn
367
422
  * const result = compute(4, 5) // 20
368
423
  */
369
- exhaustive: <T extends string | number | symbol, R extends Type>(cases: Record<T, R>) => (value: T) => R;
424
+ exhaustive: <T extends string | number | symbol, R extends Type>(cases: RequireExhaustive<T, Record<T, R>>) => (value: T) => R;
370
425
  /**
371
426
  * Create a partial match that requires a default
372
427
  * @example
@@ -417,6 +472,46 @@ declare const Match: (<T extends Type, R extends Type>(value: T) => Match<T, R>)
417
472
  withGuards: <T extends Type, R extends Type>(guards: Array<[(value: T) => boolean, R | ((value: T) => R)]>) => {
418
473
  withDefault: (defaultValue: R | ((value: T) => R)) => (value: T) => R;
419
474
  };
475
+ /**
476
+ * Pattern matching for objects with specific structure
477
+ * @example
478
+ * type Event =
479
+ * | { type: "click"; x: number; y: number }
480
+ * | { type: "keypress"; key: string }
481
+ * | { type: "hover"; element: string }
482
+ *
483
+ * const handler = Match.struct<Event, void>()
484
+ * .case({ type: "click" }, (e) => console.log(`Click at ${e.x}, ${e.y}`))
485
+ * .case({ type: "keypress", key: "Enter" }, () => console.log("Enter pressed"))
486
+ * .case({ type: "hover" }, (e) => console.log(`Hovering over ${e.element}`))
487
+ * .build()
488
+ */
489
+ struct: <T extends Type, R extends Type>() => {
490
+ case: (pattern: Pattern<T>, handler: (value: T) => R) => /*elided*/ any;
491
+ build: () => (value: T) => R;
492
+ };
493
+ /**
494
+ * Create a pattern matcher with guards and nested patterns
495
+ * @example
496
+ * type User = {
497
+ * name: string
498
+ * age: number
499
+ * permissions: string[]
500
+ * }
501
+ *
502
+ * const canAccess = Match.builder<User, boolean>()
503
+ * .when(u => u.permissions.includes("admin"), true)
504
+ * .case({ age: n => n >= 18, permissions: p => p.length > 0 }, true)
505
+ * .default(false)
506
+ * .build()
507
+ */
508
+ builder: <T extends Type, R extends Type>() => {
509
+ case: (pattern: Pattern<T>, result: PatternResult<T, R>) => /*elided*/ any;
510
+ when: (guard: (value: T) => boolean, result: PatternResult<T, R>) => /*elided*/ any;
511
+ default: (result: PatternResult<T, R>) => {
512
+ build: () => (value: T) => R;
513
+ };
514
+ };
420
515
  };
421
516
 
422
517
  /**
@@ -433,6 +528,9 @@ declare function Base<T>(type: string, body: T): T & {
433
528
  * The identifier name for Throwable type
434
529
  */
435
530
  declare const NAME: "Throwable";
531
+ /**
532
+ * @internal
533
+ */
436
534
  type ThrowableType = Error & Typeable<typeof NAME> & {
437
535
  readonly data?: unknown;
438
536
  readonly cause?: Error;
@@ -721,6 +819,356 @@ type ParseError = Error & {
721
819
  name: "ParseError";
722
820
  };
723
821
 
822
+ /**
823
+ * Type-safe error codes using template literal types
824
+ */
825
+ type ErrorCode = "VALIDATION_FAILED" | "NETWORK_ERROR" | "AUTH_REQUIRED" | "NOT_FOUND" | "PERMISSION_DENIED" | "RATE_LIMITED" | "INTERNAL_ERROR" | "BAD_REQUEST" | "CONFLICT" | "TIMEOUT";
826
+ /**
827
+ * Template literal type for error messages based on error code
828
+ */
829
+ type ErrorMessage<T extends ErrorCode> = T extends "VALIDATION_FAILED" ? `Validation failed: ${string}` : T extends "NETWORK_ERROR" ? `Network error: ${string}` : T extends "AUTH_REQUIRED" ? `Authentication required: ${string}` : T extends "NOT_FOUND" ? `Not found: ${string}` : T extends "PERMISSION_DENIED" ? `Permission denied: ${string}` : T extends "RATE_LIMITED" ? `Rate limit exceeded: ${string}` : T extends "INTERNAL_ERROR" ? `Internal server error: ${string}` : T extends "BAD_REQUEST" ? `Bad request: ${string}` : T extends "CONFLICT" ? `Conflict: ${string}` : T extends "TIMEOUT" ? `Request timeout: ${string}` : never;
830
+ /**
831
+ * HTTP status codes mapped to error codes
832
+ */
833
+ type ErrorStatus<T extends ErrorCode> = T extends "VALIDATION_FAILED" | "BAD_REQUEST" ? 400 : T extends "AUTH_REQUIRED" ? 401 : T extends "PERMISSION_DENIED" ? 403 : T extends "NOT_FOUND" ? 404 : T extends "CONFLICT" ? 409 : T extends "RATE_LIMITED" ? 429 : T extends "TIMEOUT" ? 408 : T extends "INTERNAL_ERROR" ? 500 : T extends "NETWORK_ERROR" ? 503 : 500;
834
+ /**
835
+ * Context type for each error code
836
+ */
837
+ type TypedErrorContext<T extends ErrorCode> = T extends "VALIDATION_FAILED" ? {
838
+ field: string;
839
+ value: unknown;
840
+ rule: string;
841
+ } : T extends "NETWORK_ERROR" ? {
842
+ url: string;
843
+ method: string;
844
+ statusCode?: number;
845
+ } : T extends "AUTH_REQUIRED" ? {
846
+ resource: string;
847
+ requiredRole?: string;
848
+ } : T extends "NOT_FOUND" ? {
849
+ resource: string;
850
+ id: string | number;
851
+ } : T extends "PERMISSION_DENIED" ? {
852
+ action: string;
853
+ resource: string;
854
+ userId?: string;
855
+ } : T extends "RATE_LIMITED" ? {
856
+ limit: number;
857
+ window: string;
858
+ retryAfter?: number;
859
+ } : T extends "INTERNAL_ERROR" ? {
860
+ errorId: string;
861
+ timestamp: string;
862
+ } : T extends "BAD_REQUEST" ? {
863
+ reason: string;
864
+ expected?: string;
865
+ } : T extends "CONFLICT" ? {
866
+ resource: string;
867
+ conflictingValue: string;
868
+ } : T extends "TIMEOUT" ? {
869
+ duration: number;
870
+ operation: string;
871
+ } : Record<string, unknown>;
872
+ /**
873
+ * Type-safe error class with template literal types
874
+ */
875
+ interface TypedError<T extends ErrorCode> extends Throwable {
876
+ readonly code: T;
877
+ readonly message: ErrorMessage<T>;
878
+ readonly status: ErrorStatus<T>;
879
+ readonly context: TypedErrorContext<T>;
880
+ readonly timestamp: string;
881
+ readonly traceId?: string;
882
+ }
883
+ declare const TypedError: (<T extends ErrorCode>(code: T, message: ErrorMessage<T>, context: TypedErrorContext<T>, options?: {
884
+ cause?: unknown;
885
+ traceId?: string;
886
+ }) => TypedError<T>) & {
887
+ /**
888
+ * Create a validation error
889
+ * @example
890
+ * const error = TypedError.validation("email", "test@", "must be valid email")
891
+ * // Type: TypedError<"VALIDATION_FAILED">
892
+ * // Message must match: "Validation failed: ..."
893
+ */
894
+ validation: (field: string, value: unknown, rule: string) => TypedError<"VALIDATION_FAILED">;
895
+ /**
896
+ * Create a network error
897
+ * @example
898
+ * const error = TypedError.network("https://api.example.com", "POST", 500)
899
+ * // Type: TypedError<"NETWORK_ERROR">
900
+ */
901
+ network: (url: string, method: string, statusCode?: number) => TypedError<"NETWORK_ERROR">;
902
+ /**
903
+ * Create an authentication error
904
+ * @example
905
+ * const error = TypedError.auth("/api/admin", "admin")
906
+ * // Type: TypedError<"AUTH_REQUIRED">
907
+ */
908
+ auth: (resource: string, requiredRole?: string) => TypedError<"AUTH_REQUIRED">;
909
+ /**
910
+ * Create a not found error
911
+ * @example
912
+ * const error = TypedError.notFound("user", "123")
913
+ * // Type: TypedError<"NOT_FOUND">
914
+ */
915
+ notFound: (resource: string, id: string | number) => TypedError<"NOT_FOUND">;
916
+ /**
917
+ * Create a permission denied error
918
+ * @example
919
+ * const error = TypedError.permission("delete", "post", "user123")
920
+ * // Type: TypedError<"PERMISSION_DENIED">
921
+ */
922
+ permission: (action: string, resource: string, userId?: string) => TypedError<"PERMISSION_DENIED">;
923
+ /**
924
+ * Create a rate limit error
925
+ * @example
926
+ * const error = TypedError.rateLimit(100, "1h", 3600)
927
+ * // Type: TypedError<"RATE_LIMITED">
928
+ */
929
+ rateLimit: (limit: number, window: string, retryAfter?: number) => TypedError<"RATE_LIMITED">;
930
+ /**
931
+ * Create an internal error
932
+ * @example
933
+ * const error = TypedError.internal("ERR-500-ABC123")
934
+ * // Type: TypedError<"INTERNAL_ERROR">
935
+ */
936
+ internal: (errorId: string) => TypedError<"INTERNAL_ERROR">;
937
+ /**
938
+ * Create a bad request error
939
+ * @example
940
+ * const error = TypedError.badRequest("Invalid JSON", "valid JSON object")
941
+ * // Type: TypedError<"BAD_REQUEST">
942
+ */
943
+ badRequest: (reason: string, expected?: string) => TypedError<"BAD_REQUEST">;
944
+ /**
945
+ * Create a conflict error
946
+ * @example
947
+ * const error = TypedError.conflict("email", "user@example.com")
948
+ * // Type: TypedError<"CONFLICT">
949
+ */
950
+ conflict: (resource: string, conflictingValue: string) => TypedError<"CONFLICT">;
951
+ /**
952
+ * Create a timeout error
953
+ * @example
954
+ * const error = TypedError.timeout(30000, "database query")
955
+ * // Type: TypedError<"TIMEOUT">
956
+ */
957
+ timeout: (duration: number, operation: string) => TypedError<"TIMEOUT">;
958
+ /**
959
+ * Check if a value is a TypedError
960
+ */
961
+ isTypedError: (value: unknown) => value is TypedError<ErrorCode>;
962
+ /**
963
+ * Check if a TypedError has a specific code
964
+ */
965
+ hasCode: <T extends ErrorCode>(error: TypedError<ErrorCode>, code: T) => error is TypedError<T>;
966
+ };
967
+
968
+ /**
969
+ * LazyList provides lazy evaluation for list operations.
970
+ * Operations are deferred until the list is materialized.
971
+ *
972
+ * @example
973
+ * // Basic lazy evaluation
974
+ * const result = LazyList([1, 2, 3, 4, 5])
975
+ * .map(x => x * 2)
976
+ * .filter(x => x > 5)
977
+ * .toArray() // [6, 8, 10]
978
+ *
979
+ * @example
980
+ * // Infinite sequences with take
981
+ * const fibonacci = LazyList.iterate([0, 1], ([a, b]) => [b, a + b])
982
+ * .map(([a]) => a)
983
+ * .take(10)
984
+ * .toArray() // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
985
+ */
986
+ interface LazyList<A extends Type> extends Foldable<A>, Pipe<LazyList<A>>, Serializable<LazyList<A>>, Typeable<"LazyList"> {
987
+ [Symbol.iterator](): Iterator<A>;
988
+ map<B extends Type>(f: (a: A) => B): LazyList<B>;
989
+ flatMap<B extends Type>(f: (a: A) => LazyList<B>): LazyList<B>;
990
+ filter(predicate: (a: A) => boolean): LazyList<A>;
991
+ take(n: number): LazyList<A>;
992
+ drop(n: number): LazyList<A>;
993
+ takeWhile(predicate: (a: A) => boolean): LazyList<A>;
994
+ dropWhile(predicate: (a: A) => boolean): LazyList<A>;
995
+ concat(other: LazyList<A>): LazyList<A>;
996
+ zip<B extends Type>(other: LazyList<B>): LazyList<[A, B]>;
997
+ toList(): List<A>;
998
+ toArray(): A[];
999
+ forEach(f: (a: A) => void): void;
1000
+ reduce<B extends Type>(f: (acc: B, a: A) => B, initial: B): B;
1001
+ find(predicate: (a: A) => boolean): Option<A>;
1002
+ some(predicate: (a: A) => boolean): boolean;
1003
+ every(predicate: (a: A) => boolean): boolean;
1004
+ count(): number;
1005
+ first(): Option<A>;
1006
+ last(): Option<A>;
1007
+ toString(): string;
1008
+ }
1009
+ /**
1010
+ * Lazy list implementation for efficient deferred computation
1011
+ * @example
1012
+ * // Process large datasets efficiently
1013
+ * const result = LazyList.range(1, 1000000)
1014
+ * .filter(x => x % 2 === 0)
1015
+ * .map(x => x * x)
1016
+ * .take(5)
1017
+ * .toArray() // [4, 16, 36, 64, 100]
1018
+ *
1019
+ * @example
1020
+ * // Infinite sequences
1021
+ * const primes = LazyList.iterate(2, n => n + 1)
1022
+ * .filter(isPrime)
1023
+ * .take(10)
1024
+ * .toArray() // First 10 prime numbers
1025
+ *
1026
+ * @example
1027
+ * // Combining operations
1028
+ * const evens = LazyList.range(0, 100, 2)
1029
+ * const odds = LazyList.range(1, 100, 2)
1030
+ * const combined = evens.zip(odds)
1031
+ * .map(([e, o]) => e + o)
1032
+ * .take(5)
1033
+ * .toArray() // [1, 5, 9, 13, 17]
1034
+ */
1035
+ declare const LazyList: (<A extends Type>(iterable: Iterable<A>) => LazyList<A>) & {
1036
+ /**
1037
+ * Create an empty LazyList
1038
+ * @example
1039
+ * const empty = LazyList.empty<number>()
1040
+ * empty.toArray() // []
1041
+ */
1042
+ empty: <A extends Type>() => LazyList<A>;
1043
+ /**
1044
+ * Create a LazyList from a single value
1045
+ * @example
1046
+ * const single = LazyList.of(42)
1047
+ * .map(x => x * 2)
1048
+ * .toArray() // [84]
1049
+ */
1050
+ of: <A extends Type>(value: A) => LazyList<A>;
1051
+ /**
1052
+ * Create a LazyList from multiple values
1053
+ */
1054
+ from: <A extends Type>(...values: A[]) => LazyList<A>;
1055
+ /**
1056
+ * Create an infinite LazyList by repeatedly applying a function
1057
+ * @example
1058
+ * // Powers of 2
1059
+ * const powers = LazyList.iterate(1, x => x * 2)
1060
+ * .take(10)
1061
+ * .toArray() // [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
1062
+ *
1063
+ * @example
1064
+ * // Fibonacci sequence
1065
+ * const fib = LazyList.iterate([0, 1], ([a, b]) => [b, a + b])
1066
+ * .map(([a]) => a)
1067
+ * .take(8)
1068
+ * .toArray() // [0, 1, 1, 2, 3, 5, 8, 13]
1069
+ */
1070
+ iterate: <A extends Type>(initial: A, f: (a: A) => A) => LazyList<A>;
1071
+ /**
1072
+ * Create an infinite LazyList by repeatedly calling a function
1073
+ */
1074
+ generate: <A extends Type>(f: () => A) => LazyList<A>;
1075
+ /**
1076
+ * Create a LazyList of numbers from start to end (exclusive)
1077
+ * @example
1078
+ * LazyList.range(1, 6).toArray() // [1, 2, 3, 4, 5]
1079
+ * LazyList.range(0, 10, 2).toArray() // [0, 2, 4, 6, 8]
1080
+ * LazyList.range(10, 0, -1).toArray() // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
1081
+ *
1082
+ * @example
1083
+ * // Sum of squares from 1 to 100
1084
+ * const sum = LazyList.range(1, 101)
1085
+ * .map(x => x * x)
1086
+ * .reduce((a, b) => a + b, 0) // 338350
1087
+ */
1088
+ range: (start: number, end: number, step?: number) => LazyList<number>;
1089
+ /**
1090
+ * Create a LazyList that repeats a value n times (or infinitely if n is not provided)
1091
+ */
1092
+ repeat: <A extends Type>(value: A, n?: number) => LazyList<A>;
1093
+ /**
1094
+ * Create a LazyList that cycles through an iterable infinitely
1095
+ */
1096
+ cycle: <A extends Type>(iterable: Iterable<A>) => LazyList<A>;
1097
+ };
1098
+
1099
+ /**
1100
+ * Validation rule types using template literal types
1101
+ */
1102
+ type ValidationRule = `min:${number}` | `max:${number}` | `minLength:${number}` | `maxLength:${number}` | `pattern:${string}` | `email` | `url` | `uuid` | `required` | `numeric` | `alpha` | `alphanumeric` | `date` | `future` | `past` | `in:${string}` | `notIn:${string}`;
1103
+ /**
1104
+ * Validator function type
1105
+ */
1106
+ type Validator<T> = (value: unknown) => Either<TypedError<"VALIDATION_FAILED">, T>;
1107
+ /**
1108
+ * Field validation result
1109
+ */
1110
+ type FieldValidation<T> = {
1111
+ field: string;
1112
+ value: unknown;
1113
+ result: Either<TypedError<"VALIDATION_FAILED">, T>;
1114
+ };
1115
+ /**
1116
+ * Form validation result
1117
+ */
1118
+ type FormValidation<T extends Record<string, Type>> = Either<List<TypedError<"VALIDATION_FAILED">>, T>;
1119
+ declare const Validation: (<T extends Type>(rule: ValidationRule) => Validator<T>) & {
1120
+ /**
1121
+ * Common pre-built validators
1122
+ */
1123
+ validators: {
1124
+ email: Validator<string>;
1125
+ url: Validator<string>;
1126
+ uuid: Validator<string>;
1127
+ required: Validator<string>;
1128
+ numeric: Validator<number>;
1129
+ positiveNumber: Validator<number>;
1130
+ nonEmptyString: Validator<string>;
1131
+ };
1132
+ /**
1133
+ * Create a validator from a rule string
1134
+ * @example
1135
+ * const validator = Validation.rule<number>("min:18")
1136
+ * const result = validator(25) // Right(25)
1137
+ * const error = validator(15) // Left(TypedError)
1138
+ */
1139
+ rule: <T extends Type>(rule: ValidationRule) => Validator<T>;
1140
+ /**
1141
+ * Combine multiple validators
1142
+ * @example
1143
+ * const validator = Validation.combine(
1144
+ * Validation.rule<string>("required"),
1145
+ * Validation.rule<string>("email"),
1146
+ * Validation.rule<string>("maxLength:100")
1147
+ * )
1148
+ */
1149
+ combine: <T extends Type>(...validators: Validator<T>[]) => Validator<T>;
1150
+ /**
1151
+ * Create a custom validator
1152
+ * @example
1153
+ * const isEven = Validation.custom<number>(
1154
+ * (value) => typeof value === "number" && value % 2 === 0,
1155
+ * "must be an even number"
1156
+ * )
1157
+ */
1158
+ custom: <T extends Type>(predicate: (value: unknown) => boolean, errorMessage: string) => Validator<T>;
1159
+ /**
1160
+ * Validate a form with multiple fields
1161
+ * @example
1162
+ * const schema = {
1163
+ * name: Validation.rule<string>("required"),
1164
+ * email: Validation.rule<string>("email"),
1165
+ * age: Validation.rule<number>("min:18")
1166
+ * }
1167
+ * const result = Validation.form(schema, { name: "John", email: "john@example.com", age: 25 })
1168
+ */
1169
+ form: <T extends Record<string, Type>>(schema: { [K in keyof T]: Validator<T[K]>; }, data: Record<string, unknown>) => FormValidation<T>;
1170
+ };
1171
+
724
1172
  /**
725
1173
  * Utility functions for working with Foldable data structures
726
1174
  */
@@ -776,13 +1224,20 @@ type EitherKind<E> = <A>(a: A) => Either<E, A>;
776
1224
  type TryKind = <A>(a: A) => Try<A>;
777
1225
  /**
778
1226
  * Generic container types for type-safe operations
1227
+ * @internal
779
1228
  */
780
1229
  type Mappable<T> = {
781
1230
  map<U>(f: (value: T) => U): unknown;
782
1231
  };
1232
+ /**
1233
+ * @internal
1234
+ */
783
1235
  type Flattenable = {
784
1236
  flatten(): unknown;
785
1237
  };
1238
+ /**
1239
+ * @internal
1240
+ */
786
1241
  type FlatMappable<T> = {
787
1242
  flatMap<U>(f: (value: T) => unknown): unknown;
788
1243
  };
@@ -1081,148 +1536,100 @@ declare const Lazy: (<T extends Type>(thunk: () => T) => Lazy<T>) & {
1081
1536
  };
1082
1537
 
1083
1538
  /**
1084
- * LazyList provides lazy evaluation for list operations.
1085
- * Operations are deferred until the list is materialized.
1086
- *
1087
- * @example
1088
- * // Basic lazy evaluation
1089
- * const result = LazyList([1, 2, 3, 4, 5])
1090
- * .map(x => x * 2)
1091
- * .filter(x => x > 5)
1092
- * .toArray() // [6, 8, 10]
1093
- *
1094
- * @example
1095
- * // Infinite sequences with take
1096
- * const fibonacci = LazyList.iterate([0, 1], ([a, b]) => [b, a + b])
1097
- * .map(([a]) => a)
1098
- * .take(10)
1099
- * .toArray() // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
1539
+ * Type alias for the native JavaScript Map
1540
+ * @interface
1541
+ * @module Map
1542
+ * @category Collections
1100
1543
  */
1101
- type LazyList<A extends Type> = {
1102
- [Symbol.iterator](): Iterator<A>;
1103
- map<B extends Type>(f: (a: A) => B): LazyList<B>;
1104
- flatMap<B extends Type>(f: (a: A) => LazyList<B>): LazyList<B>;
1105
- filter(predicate: (a: A) => boolean): LazyList<A>;
1106
- take(n: number): LazyList<A>;
1107
- drop(n: number): LazyList<A>;
1108
- takeWhile(predicate: (a: A) => boolean): LazyList<A>;
1109
- dropWhile(predicate: (a: A) => boolean): LazyList<A>;
1110
- concat(other: LazyList<A>): LazyList<A>;
1111
- zip<B extends Type>(other: LazyList<B>): LazyList<[A, B]>;
1112
- toList(): List<A>;
1113
- toArray(): A[];
1114
- forEach(f: (a: A) => void): void;
1115
- reduce<B extends Type>(f: (acc: B, a: A) => B, initial: B): B;
1116
- find(predicate: (a: A) => boolean): Option<A>;
1117
- some(predicate: (a: A) => boolean): boolean;
1118
- every(predicate: (a: A) => boolean): boolean;
1119
- count(): number;
1120
- first(): Option<A>;
1121
- last(): Option<A>;
1122
- };
1544
+ type ESMapType<K, V> = Map<K, V>;
1123
1545
  /**
1124
- * Lazy list implementation for efficient deferred computation
1125
- * @example
1126
- * // Process large datasets efficiently
1127
- * const result = LazyList.range(1, 1000000)
1128
- * .filter(x => x % 2 === 0)
1129
- * .map(x => x * x)
1130
- * .take(5)
1131
- * .toArray() // [4, 16, 36, 64, 100]
1132
- *
1133
- * @example
1134
- * // Infinite sequences
1135
- * const primes = LazyList.iterate(2, n => n + 1)
1136
- * .filter(isPrime)
1137
- * .take(10)
1138
- * .toArray() // First 10 prime numbers
1546
+ * Reference to the native JavaScript Map
1547
+ * @module Map
1548
+ * @category Collections
1549
+ */
1550
+ declare const ESMap: MapConstructor;
1551
+
1552
+ /**
1553
+ * A mutable reference container that holds a value of type A.
1554
+ * This provides controlled mutability in a functional context.
1139
1555
  *
1140
1556
  * @example
1141
- * // Combining operations
1142
- * const evens = LazyList.range(0, 100, 2)
1143
- * const odds = LazyList.range(1, 100, 2)
1144
- * const combined = evens.zip(odds)
1145
- * .map(([e, o]) => e + o)
1146
- * .take(5)
1147
- * .toArray() // [1, 5, 9, 13, 17]
1557
+ * const counter = Ref(0)
1558
+ * counter.get() // 0
1559
+ * counter.set(5)
1560
+ * counter.get() // 5
1561
+ * counter.update(n => n + 1)
1562
+ * counter.get() // 6
1148
1563
  */
1149
- declare const LazyList: (<A extends Type>(iterable: Iterable<A>) => LazyList<A>) & {
1564
+ interface Ref<A> {
1150
1565
  /**
1151
- * Create an empty LazyList
1152
- * @example
1153
- * const empty = LazyList.empty<number>()
1154
- * empty.toArray() // []
1566
+ * Get the current value
1155
1567
  */
1156
- empty: <A extends Type>() => LazyList<A>;
1568
+ get(): A;
1157
1569
  /**
1158
- * Create a LazyList from a single value
1159
- * @example
1160
- * const single = LazyList.of(42)
1161
- * .map(x => x * 2)
1162
- * .toArray() // [84]
1570
+ * Set a new value
1163
1571
  */
1164
- of: <A extends Type>(value: A) => LazyList<A>;
1572
+ set(value: A): void;
1165
1573
  /**
1166
- * Create a LazyList from multiple values
1574
+ * Update the value using a function
1167
1575
  */
1168
- from: <A extends Type>(...values: A[]) => LazyList<A>;
1576
+ update(f: (current: A) => A): void;
1169
1577
  /**
1170
- * Create an infinite LazyList by repeatedly applying a function
1171
- * @example
1172
- * // Powers of 2
1173
- * const powers = LazyList.iterate(1, x => x * 2)
1174
- * .take(10)
1175
- * .toArray() // [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
1176
- *
1177
- * @example
1178
- * // Fibonacci sequence
1179
- * const fib = LazyList.iterate([0, 1], ([a, b]) => [b, a + b])
1180
- * .map(([a]) => a)
1181
- * .take(8)
1182
- * .toArray() // [0, 1, 1, 2, 3, 5, 8, 13]
1578
+ * Update and return the old value
1183
1579
  */
1184
- iterate: <A extends Type>(initial: A, f: (a: A) => A) => LazyList<A>;
1580
+ getAndSet(value: A): A;
1185
1581
  /**
1186
- * Create an infinite LazyList by repeatedly calling a function
1582
+ * Update and return the new value
1187
1583
  */
1188
- generate: <A extends Type>(f: () => A) => LazyList<A>;
1584
+ updateAndGet(f: (current: A) => A): A;
1189
1585
  /**
1190
- * Create a LazyList of numbers from start to end (exclusive)
1191
- * @example
1192
- * LazyList.range(1, 6).toArray() // [1, 2, 3, 4, 5]
1193
- * LazyList.range(0, 10, 2).toArray() // [0, 2, 4, 6, 8]
1194
- * LazyList.range(10, 0, -1).toArray() // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
1195
- *
1196
- * @example
1197
- * // Sum of squares from 1 to 100
1198
- * const sum = LazyList.range(1, 101)
1199
- * .map(x => x * x)
1200
- * .reduce((a, b) => a + b, 0) // 338350
1586
+ * Update and return the old value
1201
1587
  */
1202
- range: (start: number, end: number, step?: number) => LazyList<number>;
1588
+ getAndUpdate(f: (current: A) => A): A;
1203
1589
  /**
1204
- * Create a LazyList that repeats a value n times (or infinitely if n is not provided)
1590
+ * Compare and swap - only updates if current value equals expected
1205
1591
  */
1206
- repeat: <A extends Type>(value: A, n?: number) => LazyList<A>;
1592
+ compareAndSet(expected: A, newValue: A): boolean;
1207
1593
  /**
1208
- * Create a LazyList that cycles through an iterable infinitely
1594
+ * Modify the value and return a result
1209
1595
  */
1210
- cycle: <A extends Type>(iterable: Iterable<A>) => LazyList<A>;
1211
- };
1596
+ modify<B>(f: (current: A) => [A, B]): B;
1597
+ }
1598
+ /**
1599
+ * Creates a new mutable reference containing the given value
1600
+ */
1601
+ declare function Ref<A extends Type>(initial: A): Ref<A>;
1602
+ declare namespace Ref {
1603
+ var of: typeof Ref;
1604
+ }
1212
1605
 
1213
1606
  /**
1214
- * Type alias for the native JavaScript Map
1215
- * @interface
1216
- * @module Map
1217
- * @category Collections
1607
+ * Parameters for creating a Valuable instance
1218
1608
  */
1219
- type ESMapType<K, V> = Map<K, V>;
1609
+ type ValuableParams<Tag extends string, T, V> = {
1610
+ _tag: Tag;
1611
+ impl: T;
1612
+ value: V;
1613
+ };
1220
1614
  /**
1221
- * Reference to the native JavaScript Map
1222
- * @module Map
1223
- * @category Collections
1615
+ * Represents a type that can extract its inner value. Creates a Valuable wrapper that adds value extraction capabilities.
1616
+ * @param params - Configuration parameters
1617
+ * @module Valuable
1618
+ * @category Utilities
1224
1619
  */
1225
- declare const ESMap: MapConstructor;
1620
+ declare function Valuable<Tag extends string, V, T = object>(params: ValuableParams<Tag, T, V>): T & {
1621
+ toValue: () => {
1622
+ _tag: Tag;
1623
+ value: V;
1624
+ };
1625
+ _tag: Tag;
1626
+ };
1627
+ type Valuable<Tag extends string, V, T = object> = Typeable<Tag, T> & {
1628
+ toValue: () => {
1629
+ _tag: Tag;
1630
+ value: V;
1631
+ };
1632
+ };
1226
1633
 
1227
1634
  /**
1228
1635
  * Stack data structure - Last In, First Out (LIFO)
@@ -1326,4 +1733,4 @@ declare const Stack: (<A extends Type>(values?: A[]) => Stack<A>) & {
1326
1733
  fromBinary: <A>(binary: string) => Stack<A>;
1327
1734
  };
1328
1735
 
1329
- export { type Async, Base, BoundedNumber, BoundedString, Brand, type CancellationToken, type CancellationTokenSource, Companion, Cond, ESMap, type ESMapType, Either, type EitherKind, EmailAddress, type ErrorChainElement, type ErrorFormatterOptions, type ErrorWithTaskInfo, FPromise, Foldable, FoldableUtils, FunctypeBase, HKT, ISO8601Date, Identity, IntegerNumber, type Kind, Lazy, LazyList, Lazy as LazyType, List, type ListKind, Match, Matchable, NAME, NonEmptyString, NonNegativeNumber, Option, type OptionKind, ParseError, PatternString, Pipe, PositiveInteger, PositiveNumber, Serializable, Stack, type Sync, type TaggedThrowable, Task, type TaskErrorInfo, TaskException, type TaskInfo, type TaskParams, TaskResult, Throwable, type ThrowableType, Traversable, Try, type TryKind, Type, Typeable, UUID, type UniversalContainer, UrlString, ValidatedBrand, Valuable, createCancellationTokenSource, createErrorSerializer, formatError, formatStackTrace, isTaggedThrowable, safeStringify };
1736
+ export { type Async, Base, BoundedNumber, BoundedString, Brand, type CancellationToken, type CancellationTokenSource, Companion, Cond, ESMap, type ESMapType, Either, type EitherKind, EmailAddress, type ErrorChainElement, type ErrorCode, type ErrorFormatterOptions, type ErrorMessage, type ErrorStatus, type ErrorWithTaskInfo, Extractable, FPromise, type FieldValidation, Foldable, FoldableUtils, type FormValidation, FunctypeBase, HKT, ISO8601Date, Identity, IntegerNumber, type Kind, Lazy, LazyList, Lazy as LazyType, List, type ListKind, Match, Matchable, NAME, NonEmptyString, NonNegativeNumber, Option, type OptionKind, ParseError, PatternString, Pipe, PositiveInteger, PositiveNumber, Ref, Ref as RefType, Serializable, Stack, type Sync, type TaggedThrowable, Task, type TaskErrorInfo, TaskException, type TaskInfo, type TaskParams, TaskResult, Throwable, type ThrowableType, Traversable, Try, type TryKind, Type, TypedError, type TypedErrorContext, UUID, type UniversalContainer, UrlString, ValidatedBrand, Validation, type ValidationRule, type Validator, Valuable, type ValuableParams, createCancellationTokenSource, createErrorSerializer, formatError, formatStackTrace, isTaggedThrowable, safeStringify };