I was having trouble using nested generics in the model initializer:
Here's a slightly contrived example:
import { Schema, model } from 'mongoose';
import type { TestDocument, TestModel } from '../interfaces/models.gen';
const TestSchema = new Schema({
test: {
type: String,
default: 'hello',
required: true,
},
});
TestSchema.methods = {
isActive() {
return true;
},
};
type SomeGenericType<T = {}> = TestDocument & T;
// The problem occurs here because the generic params for model don't match the regex defined in tsReader.ts
export const Test = model<SomeGenericType<{}>, TestModel>('Test', TestSchema);
This causes the generation of TestMethods to default to (this: TestDocument, ...args: any[]) => any, rather than its true type.
I was having trouble using nested generics in the model initializer:
Here's a slightly contrived example:
This causes the generation of TestMethods to default to
(this: TestDocument, ...args: any[]) => any
, rather than its true type.Before fix:
After fix:
Fix
This PR fixes it by adjusting the regex to allow (& ignore) anything in the generic params for the model initializer.