@@ -1,4 +1,5 @@
|
||||
import { Effectable, Schema, type Pipeable } from "effect"
|
||||
import { Effectable, Option, Schema, Subscribable, SubscriptionRef, type ParseResult, type Pipeable } from "effect"
|
||||
import type * as PropertyPath from "./PropertyPath.js"
|
||||
|
||||
|
||||
export const TypeId: unique symbol = Symbol.for("effect-fc/types/Form")
|
||||
@@ -7,9 +8,20 @@ export type TypeId = typeof TypeId
|
||||
export interface Form<in out A, in out I = A, out R = never>
|
||||
extends Pipeable.Pipeable {
|
||||
readonly schema: Schema.Schema<A, I, R>
|
||||
readonly valueRef: SubscriptionRef.SubscriptionRef<A>
|
||||
readonly errorSubscribable: Subscribable.Subscribable<Option.Option<ParseResult.ParseError>>
|
||||
|
||||
useRef<P extends PropertyPath.Paths<A>>(path: P): SubscriptionRef.SubscriptionRef<PropertyPath.ValueFromPath<A, P>>
|
||||
useIssues(path: PropertyPath.Paths<A>): readonly ParseResult.ParseIssue[]
|
||||
}
|
||||
|
||||
class FormImpl<in out A, in out I, out R>
|
||||
extends Effectable.Class<> implements Form<> {
|
||||
extends Effectable.Class<> implements Form<A, I, R> {
|
||||
readonly [TypeId]: TypeId = TypeId
|
||||
}
|
||||
|
||||
|
||||
const TestSchema = Schema.Struct({
|
||||
name: Schema.String
|
||||
})
|
||||
declare const form: Form<typeof TestSchema["Type"], typeof TestSchema["Encoded"], typeof TestSchema["Context"]>
|
||||
|
||||
@@ -3,9 +3,9 @@ import { Array, Function, Option, Predicate } from "effect"
|
||||
|
||||
type Prev = readonly [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
|
||||
export type Paths<T, D extends number = 5, Seen = never> = [] | (
|
||||
D extends never ? [] :
|
||||
T extends Seen ? [] :
|
||||
export type Paths<T, D extends number = 5, Seen = never> = readonly [] | (
|
||||
D extends never ? readonly [] :
|
||||
T extends Seen ? readonly [] :
|
||||
T extends readonly any[] ? ArrayPaths<T, D, Seen | T> :
|
||||
T extends object ? ObjectPaths<T, D, Seen | T> :
|
||||
never
|
||||
@@ -13,8 +13,8 @@ export type Paths<T, D extends number = 5, Seen = never> = [] | (
|
||||
|
||||
export type ArrayPaths<T extends readonly any[], D extends number, Seen> = {
|
||||
[K in keyof T as K extends number ? K : never]:
|
||||
| [K]
|
||||
| [K, ...Paths<T[K], Prev[D], Seen>]
|
||||
| readonly [K]
|
||||
| readonly [K, ...Paths<T[K], Prev[D], Seen>]
|
||||
} extends infer O
|
||||
? O[keyof O]
|
||||
: never
|
||||
@@ -22,13 +22,13 @@ export type ArrayPaths<T extends readonly any[], D extends number, Seen> = {
|
||||
export type ObjectPaths<T extends object, D extends number, Seen> = {
|
||||
[K in keyof T as K extends string | number | symbol ? K : never]-?:
|
||||
NonNullable<T[K]> extends infer V
|
||||
? [K] | [K, ...Paths<V, Prev[D], Seen>]
|
||||
? readonly [K] | readonly [K, ...Paths<V, Prev[D], Seen>]
|
||||
: never
|
||||
} extends infer O
|
||||
? O[keyof O]
|
||||
: never
|
||||
|
||||
export type ValueFromPath<T, P extends any[]> = P extends [infer Head, ...infer Tail]
|
||||
export type ValueFromPath<T, P extends readonly any[]> = P extends [infer Head, ...infer Tail]
|
||||
? Head extends keyof T
|
||||
? ValueFromPath<T[Head], Tail>
|
||||
: T extends readonly any[]
|
||||
|
||||
Reference in New Issue
Block a user