extendSchemable work
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julien Valverdé
2024-01-09 03:13:53 +01:00
parent 6fe782208d
commit 2d25c95a0a
2 changed files with 9 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
import { AbstractClass } from "type-fest" import { AbstractClass, Class } from "type-fest"
import { z } from "zod" import { z } from "zod"
import { SchemableClass } from "." import { SchemableClass } from "."
import { StaticMembers } from "./util" import { StaticMembers } from "./util"
@@ -43,6 +43,12 @@ export function extendSchemable<
defaultValuesApplier: (defaultValues: ExtendDefaultValues) => DefaultValues, defaultValuesApplier: (defaultValues: ExtendDefaultValues) => DefaultValues,
) { ) {
type ClassKind<T, Arguments extends unknown[]> = (
C extends Class<any>
? Class<T, Arguments>
: AbstractClass<T, Arguments>
)
const schema = schemaApplier(extend.schema) const schema = schemaApplier(extend.schema)
const defaultValues = defaultValuesApplier(extend.defaultValues) const defaultValues = defaultValuesApplier(extend.defaultValues)
@@ -53,7 +59,7 @@ export function extendSchemable<
static readonly defaultValues = defaultValues static readonly defaultValues = defaultValues
readonly defaultValues = defaultValues readonly defaultValues = defaultValues
} as unknown as ( } as unknown as (
AbstractClass< ClassKind<
Omit<C, "schema" | "defaultValues" | keyof ExtendSchemaValues> & Omit<C, "schema" | "defaultValues" | keyof ExtendSchemaValues> &
{ {
readonly schema: typeof schema, readonly schema: typeof schema,

View File

@@ -16,7 +16,7 @@ class Test1 {
const Test2 = extendSchemable( const Test2 = extendSchemable(
Test1, Test1,
schema => schema.extend({ prout: z.literal("ruquier"), ruquier: z.number() }), schema => schema.extend({ prout: z.literal("ruquier"), ruquier: z.number() }),
defaultValues => ({ prout: "ruquier" as const }), () => ({ prout: "ruquier" as const }),
) )
Test2.defaultValues Test2.defaultValues