MutableClass
This commit is contained in:
@@ -6,11 +6,11 @@ import type { Static } from "../.."
|
||||
import type { HasFields, MissingSelfGeneric } from "./util"
|
||||
|
||||
|
||||
type TMutableClass<
|
||||
export type TMutableClass<
|
||||
Self,
|
||||
Fields extends Struct.Fields,
|
||||
Fields extends Struct.Fields,
|
||||
I, R, C,
|
||||
Inherited extends object,
|
||||
Inherited,
|
||||
Proto,
|
||||
> = (
|
||||
Constructor<
|
||||
@@ -22,13 +22,32 @@ type TMutableClass<
|
||||
|
||||
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) => (
|
||||
<Fields extends Struct.Fields>(
|
||||
fieldsOr: Fields | HasFields<Fields>,
|
||||
annotations?: Annotations.Schema<Self>
|
||||
annotations?: Annotations.Schema<Self>,
|
||||
): (
|
||||
[Self] extends [never]
|
||||
? MissingSelfGeneric<"Class">
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
export { MutableClass } from "./MutableClass"
|
||||
export type { TMutableClass } from "./MutableClass"
|
||||
|
||||
@@ -3,8 +3,9 @@ import { MutableClass } from "./MutableClass"
|
||||
|
||||
|
||||
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 })
|
||||
user.id = 2n
|
||||
const user1 = new User({ id: 1n, role: "BasicUser" })
|
||||
user1.id = 2n
|
||||
|
||||
Reference in New Issue
Block a user