Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type "safes" #573

Open
ssalbdivad opened this issue Dec 31, 2022 · 0 comments
Open

type "safes" #573

ssalbdivad opened this issue Dec 31, 2022 · 0 comments
Projects

Comments

@ssalbdivad
Copy link
Member

ssalbdivad commented Dec 31, 2022

Would be a box for types that allowed automatic validation on mutations. Some preliminary brainstorming I did for it:

// @ts-nocheck

type User = {
    name: string
    age: number
}

// Ideas for TypeSafes
//  Keep data in a class or proxy, could gaurantee mutations

type ModelBox<T> = {
    readonly data: T
    readonly set: (data: T) => xor<Data<T>, Problems>
    readonly update: (fn: (old: T) => T) => xor<Data<T>, Problems>
    readonly to: {
        [k in outMorphs]: () => ModelBox<outMorphs<T>>
    }
}

const userOne = model.user.box({}) as ModelBox<User>

const { data, problems } = user.set({ name: "foo", age: 15 })
const result = user.update((old) => ({ ...old, age: 20 }))

type ModelProxy<T> = {
    get: Readonly<T>
    // direct mutations managed through proxy, autovalidated updates
    set: T
}

const user2 = {} as ModelProxy<User>

const n = user.get.age

user2.set.age = 5

// is this possible?
type ValidatedModel<T> = T & {
    $: {
        eject: () => T
    }
}

const user = {} as ValidatedModel<User>

const n = user.age

//  runs setter
user.age = 5
@ssalbdivad ssalbdivad created this issue from a note in arktype (Backlog) Dec 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
arktype
Backlog
Development

No branches or pull requests

1 participant