84 lines
2.0 KiB
TypeScript
84 lines
2.0 KiB
TypeScript
import { Implements, TraitExpression } from "@thilawyn/traitify-ts"
|
|
import { z } from "zod"
|
|
import { ZodSchemaClassBuilder } from "./ZodSchemaClass"
|
|
import { dejsonify, jsonify } from "./schema/jsonify"
|
|
import { ObservableZodSchemaObject } from "./traits/ObservableZodSchemaObject"
|
|
import { AbstractClass, Class } from "type-fest"
|
|
|
|
|
|
const exp = new ZodSchemaClassBuilder(TraitExpression.NullSuperclass, [])
|
|
.extends(class {})
|
|
.schema({
|
|
schema: z.object({
|
|
/** User ID */
|
|
id: z.bigint(),
|
|
|
|
/** Username */
|
|
name: z.string(),
|
|
}),
|
|
|
|
defaultValues: { id: -1n },
|
|
})
|
|
.jsonifiable({
|
|
jsonifySchema: s => s.extend({
|
|
id: jsonify.bigint(s.shape.id)
|
|
}),
|
|
|
|
dejsonifySchema: s => s.extend({
|
|
id: dejsonify.bigint(s.shape.id)
|
|
}),
|
|
})
|
|
.expresses(ObservableZodSchemaObject)
|
|
.build()
|
|
|
|
@exp.staticImplements
|
|
class User extends exp.extends implements Implements<typeof exp> {}
|
|
|
|
User.defaultValues
|
|
const inst = User.create({ id: 1n, name: "" })
|
|
// console.log(inst)
|
|
const jsonifiedUser = await inst.jsonifyPromise()
|
|
|
|
|
|
class SubTest extends User.extend({
|
|
schema: ({ schema }) => schema.extend({
|
|
prout: z.string()
|
|
}),
|
|
|
|
defaultValues: v => v,
|
|
}) {}
|
|
|
|
const subInst = await SubTest.createPromise({ name: "", prout: "" })
|
|
// console.log(subInst)
|
|
|
|
|
|
class Box<T> {
|
|
declare ["constructor"]: typeof Box
|
|
constructor(public value: T) {}
|
|
|
|
otherMethod() {
|
|
return "prout" as const
|
|
}
|
|
|
|
newBox<
|
|
C extends Class<I, ConstructorParameters<typeof Box>>,
|
|
I extends Box<any>,
|
|
T,
|
|
>(
|
|
// this: Omit<BaseClass<T>, "constructor"> & { ["constructor"]: Class<I> }
|
|
this: { ["constructor"]: C | Class<I, ConstructorParameters<typeof Box>> },
|
|
value: T,
|
|
) {
|
|
// this.otherMethod()
|
|
return new this.constructor(value)
|
|
}
|
|
}
|
|
|
|
class UnrelatedClass {}
|
|
|
|
class SuperBox<T> extends Box<T> {
|
|
declare ["constructor"]: typeof SuperBox
|
|
}
|
|
|
|
const value = new SuperBox(69).newBox(69)
|