Reducing duplication of static methods in schemas
I would like to have a static
Notice the duplicated line:
I wonder how to make it less repetitive.
I tried to create a base class and extend it, but it seems that the static method is not being recognized:
Is it possible to inherit static methods from a base Schema.Class?
If not, is there an alternative approach to reduce this duplication?
assertsasserts method on each schema, like so:export class PendingEnrollment extends Schema.Class<PendingEnrollment>('PendingEnrollment')({
status: Schema.Literal('Pending'),
id: EnrollmentId,
createdAt: Schema.Date,
}) {
static asserts: Schema.Schema.ToAsserts<typeof this> = Schema.asserts(this);
}
export class ActiveEnrollment extends Schema.Class<ActiveEnrollment>('ActiveEnrollment')({
status: Schema.Literal('Active'),
id: EnrollmentId,
createdAt: Schema.Date,
activatedAt: Schema.Date,
}) {
static asserts: Schema.Schema.ToAsserts<typeof this> = Schema.asserts(this);
}
export class InactiveEnrollment extends Schema.Class<ActiveEnrollment>('InactiveEnrollment')({
status: Schema.Literal('Inactive'),
id: EnrollmentId,
createdAt: Schema.Date,
activatedAt: Schema.Date,
deactivatedAt: Schema.Date,
}) {
static asserts: Schema.Schema.ToAsserts<typeof this> = Schema.asserts(this);
}export class PendingEnrollment extends Schema.Class<PendingEnrollment>('PendingEnrollment')({
status: Schema.Literal('Pending'),
id: EnrollmentId,
createdAt: Schema.Date,
}) {
static asserts: Schema.Schema.ToAsserts<typeof this> = Schema.asserts(this);
}
export class ActiveEnrollment extends Schema.Class<ActiveEnrollment>('ActiveEnrollment')({
status: Schema.Literal('Active'),
id: EnrollmentId,
createdAt: Schema.Date,
activatedAt: Schema.Date,
}) {
static asserts: Schema.Schema.ToAsserts<typeof this> = Schema.asserts(this);
}
export class InactiveEnrollment extends Schema.Class<ActiveEnrollment>('InactiveEnrollment')({
status: Schema.Literal('Inactive'),
id: EnrollmentId,
createdAt: Schema.Date,
activatedAt: Schema.Date,
deactivatedAt: Schema.Date,
}) {
static asserts: Schema.Schema.ToAsserts<typeof this> = Schema.asserts(this);
}Notice the duplicated line:
static asserts: Schema.Schema.ToAsserts<typeof this> = Schema.asserts(this);static asserts: Schema.Schema.ToAsserts<typeof this> = Schema.asserts(this);I wonder how to make it less repetitive.
I tried to create a base class and extend it, but it seems that the static method is not being recognized:
export class State extends Schema.Class<State>('State')({}) {
static asserts: Schema.Schema.ToAsserts<typeof this> = Schema.asserts(this);
}
export class PendingEnrollment extends State.extend<PendingEnrollment>('PendingEnrollment')({
status: Schema.Literal('Pending'),
id: EnrollmentId,
createdAt: Schema.Date,
}) {}
PendingEnrollment.asserts; // TS2339: Property asserts; does not exist on type typeof PendingEnrollmentexport class State extends Schema.Class<State>('State')({}) {
static asserts: Schema.Schema.ToAsserts<typeof this> = Schema.asserts(this);
}
export class PendingEnrollment extends State.extend<PendingEnrollment>('PendingEnrollment')({
status: Schema.Literal('Pending'),
id: EnrollmentId,
createdAt: Schema.Date,
}) {}
PendingEnrollment.asserts; // TS2339: Property asserts; does not exist on type typeof PendingEnrollmentIs it possible to inherit static methods from a base Schema.Class?
If not, is there an alternative approach to reduce this duplication?
