How to get a type alias from a class? #898
-
Hi. Say I wanted to infer/retrieve a type alias from my class; how would I achieve that? Context: This is my class: import {
prop,
getModelForClass,
post,
ModelOptions,
Severity,
} from '@typegoose/typegoose';
import mongoose from 'mongoose';
@post<LoanClass>('save', function (doc) {
if (doc) {
doc.id = doc._id.toString();
doc._id = doc.id;
}
})
@post<LoanClass[]>(/^find/, function (docs) {
// @ts-ignore
if (this.op === 'find') {
docs.forEach((doc) => {
doc.id = doc._id.toString();
doc._id = doc.id;
});
}
})
@ModelOptions({
schemaOptions: {
timestamps: true,
collection: 'loans',
},
options: {
allowMixed: Severity.ALLOW,
},
})
class LoanClass {
@prop({ required: true })
lenderAddress: string;
@prop({ required: true })
loanToken: string;
@prop({ required: true })
loanAmount: string;
@prop({ required: true })
collateralToken: string;
@prop({ required: true })
collateralAmount: string;
@prop({ required: true })
liquidationThreshold: string;
@prop({ required: true })
initialThreshold: string;
@prop({ required: true })
loanRepayDeadline: Date;
@prop({ required: true })
loanRequestDeadline: Date;
@prop({ required: true })
interestRate: string;
@prop({ required: true })
managerNFT: string;
@prop({ default: 'v1' })
managerNFTVersion: string;
_id: mongoose.Types.ObjectId | string;
id: string;
}
const Loan = getModelForClass(LoanClass);
export { Loan, LoanClass }; And I want to be able to use the inferred type alias when populating an array like so: let some_array: TLoan[]; // how can I get the type alias for this
some_array = [
{
collateralAmount: '',
collateralToken: '',
loanAmount: '',
loanToken: '',
loanRepayDeadline: new Date(),
initialThreshold: '',
interestRate: '',
liquidationThreshold: '',
lenderAddress: '',
loanRequestDeadline: new Date(),
managerNFT: '',
managerNFTVersion: '',
}
]; While I have you, do you mind having a look at that class definition, and telling me if I'm doing something the wrong way. I'm new to Typgoose, and navigating the docs is kinda hard. For example, understanding the Thanks for the consideration. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
i am not quite sure what you mean with "type alias", but if you are referring to having a type of the class that does not include functions, then maybe look at |
Beta Was this translation helpful? Give feedback.
-
Hi, @hasezoey Thanks for the reply. And yes, that was exactly what I wanted. i.e. Basically, what I wanted was just a way to type my function params, and different arrays but adhering to the class's shape. Thanks a lot. 🙂 Also, if it's no bother, do you mind having a look at that class definition. From first glance, does it look like I'm doing something wrong? For example, I see from the docs you use the |
Beta Was this translation helpful? Give feedback.
i am not quite sure what you mean with "type alias", but if you are referring to having a type of the class that does not include functions, then maybe look at
FilterOutFunctionKeys
, otherwise please provide more specifics on what that type should be