I've been involved in a TypeScript project that utilizes MongoDB for data persistence. Eliminating the use of any
is one of our main objectives.
Here's an excerpt of code that defines a part of the mongoose schema:
priceMax: {
max: 10000000,
min: 0,
required: function (this: FeePricing & Document) {
return this.priceMin === undefined;
},
type: Number,
validate: [
{
message: 'Max price cannot be lower than min price',
validator: function (v: number) {
if (this.priceMax === null || this.priceMax === undefined) return true;
return this.priceMin ? v >= this.priceMin : v >= 0;
},
},
{
message: 'Max price cannot be higher than 50000 for this feeType',
validator: function (v: number) {
return !(feeTypesWithoutMaxLimit.includes(this.feeType) && v > 50000);
},
},
],
},
priceMin: {
max: 10000000,
min: 0,
required: function () {
return this.priceMax === undefined;
},
type: Number,
validate: {
message: 'priceMin cannot be higher than priceMax',
validator: function (v: number) {
return this.priceMax ? v <= this.priceMax : v >= 0;
},
},
},
updatedAt: { type: Date },
updatedBy: { type: String },
I have a decent understanding of what the functions are designed to do, but the usage of types within them confuses me.
How can I eliminate the need for this as any
? Wouldn't it make sense to just utilize FeePricing
as the type - like (this as FeePricing)
? Is FeePricing
essentially another type specific to my app [, with both priceMin
and priceMax
] combined with a Document
interface? How does ReactJS's Document
factor into this situation? Why is it necessary? Is this
within validate
referencing the previously defined type FeePricing & Document
?
Appreciate your insights