MutableClass

This commit is contained in:
Julien Valverdé
2024-06-10 18:26:39 +02:00
parent 9146f539b8
commit 0b2c56be64
3 changed files with 29 additions and 8 deletions

View File

@@ -6,11 +6,11 @@ import type { Static } from "../.."
import type { HasFields, MissingSelfGeneric } from "./util" import type { HasFields, MissingSelfGeneric } from "./util"
type TMutableClass< export type TMutableClass<
Self, Self,
Fields extends Struct.Fields, Fields extends Struct.Fields,
I, R, C, I, R, C,
Inherited extends object, Inherited,
Proto, Proto,
> = ( > = (
Constructor< Constructor<
@@ -22,13 +22,32 @@ type TMutableClass<
ConstructorParameters<Class<Self, Fields, I, R, C, Inherited, Proto>> ConstructorParameters<Class<Self, Fields, I, R, C, Inherited, Proto>>
> & > &
Static<Class<Self, Fields, I, R, C, Inherited, Proto>>
Omit<
Static<Class<Self, Fields, I, R, C, Inherited, Proto>>,
"extend"
> & {
extend<Extended = never>(identifier: string): <newFields extends Struct.Fields>(
fields: newFields | HasFields<newFields>,
annotations?: Annotations.Schema<Extended>,
) => [Extended] extends [never]
? MissingSelfGeneric<"Base.extend">
: TMutableClass<
Extended,
Fields & newFields,
I & Struct.Encoded<newFields>,
R | Struct.Context<newFields>,
C & Struct.Constructor<newFields>,
Self,
Proto
>
}
) )
export const MutableClass = <Self = never>(identifier: string) => ( export const MutableClass = <Self = never>(identifier: string) => (
<Fields extends Struct.Fields>( <Fields extends Struct.Fields>(
fieldsOr: Fields | HasFields<Fields>, fieldsOr: Fields | HasFields<Fields>,
annotations?: Annotations.Schema<Self> annotations?: Annotations.Schema<Self>,
): ( ): (
[Self] extends [never] [Self] extends [never]
? MissingSelfGeneric<"Class"> ? MissingSelfGeneric<"Class">

View File

@@ -1 +1,2 @@
export { MutableClass } from "./MutableClass" export { MutableClass } from "./MutableClass"
export type { TMutableClass } from "./MutableClass"

View File

@@ -3,8 +3,9 @@ import { MutableClass } from "./MutableClass"
class User extends MutableClass<User>("User")({ class User extends MutableClass<User>("User")({
id: S.BigIntFromSelf id: S.BigIntFromSelf,
role: S.Union(S.Literal("BasicUser"), S.Literal("Admin")),
}) {} }) {}
const user = new User({ id: 1n }) const user1 = new User({ id: 1n, role: "BasicUser" })
user.id = 2n user1.id = 2n