Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript not working with model methods #151

Open
Paso opened this issue Feb 12, 2024 · 1 comment
Open

Typescript not working with model methods #151

Paso opened this issue Feb 12, 2024 · 1 comment

Comments

@Paso
Copy link

Paso commented Feb 12, 2024

Creating a schema with methods as in instructions from mongoose and the README doesn't work.

import { Schema } from 'mongoose';
import mongoose_delete, { SoftDeleteDocument, SoftDeleteModel } from 'mongoose-delete';

interface ITest extends SoftDeleteDocument {
  test: string;
}
interface ITestMethods {
  testMethod(): string;
}
// eslint-disable-next-line @typescript-eslint/ban-types
interface TestModel extends SoftDeleteModel<ITest, {}, ITestMethods> {
  testStatic(): string;
}
const schema = new Schema<ITest, TestModel, ITestMethods>({
  test: {
    type: String,
    required: true,
  },
});
schema.plugin(mongoose_delete);

Part of the problem is that the SoftDeleteModel generic doesn't support all the arguments of the mongoose Model, so I tried adding the third argument to the Model generic

    interface SoftDeleteModel<T extends Omit<mongoose.Document, "delete">, QueryHelpers = {}, TInstanceMethods={}>
        extends mongoose.Model<T, QueryHelpers, TInstanceMethods>

but that only resulted in the following incomprehensible error:

Argument of type '(schema: Schema<any, Model<any, any, any, any, any, any>, {}, {}, {}, {}, DefaultSchemaOptions, { [x: string]: unknown; }, Document<unknown, {}, FlatRecord<{ [x: string]: unknown; }>> & FlatRecord<...> & Required<...>>, options?: Partial<...> | undefined) => void' is not assignable to parameter of type 'PluginFunction<ITest, TestModel, any, any, any, any>'.
  Types of parameters 'schema' and 'schema' are incompatible.
    Type 'Schema<ITest, TestModel, any, any, any, any, DefaultSchemaOptions, ITest, Document<unknown, {}, FlatRecord<ITest>> & FlatRecord<...> & { ...; }>' is not assignable to type 'Schema<any, Model<any, any, any, any, any, any>, {}, {}, {}, {}, DefaultSchemaOptions, { [x: string]: unknown; }, Document<unknown, {}, FlatRecord<{ [x: string]: unknown; }>> & FlatRecord<...> & Required<...>>'.
      Types of property 'obj' are incompatible.
        Type '{ delete?: SchemaDefinitionProperty<(deleteBy?: string | ObjectId | Callback<ITest, ITest> | undefined, fn?: Callback<ITest, ITest> | undefined) => Promise<...>, ITest> | undefined; ... 7 more ...; test?: SchemaDefinitionProperty<...> | undefined; }' is not assignable to type '{ [path: string]: SchemaDefinitionProperty<undefined, any>; } | { [x: string]: SchemaDefinitionProperty<any, any> | undefined; }'.
          Type '{ delete?: SchemaDefinitionProperty<(deleteBy?: string | ObjectId | Callback<ITest, ITest> | undefined, fn?: Callback<ITest, ITest> | undefined) => Promise<...>, ITest> | undefined; ... 7 more ...; test?: SchemaDefinitionProperty<...> | undefined; }' is not assignable to type '{ [x: string]: SchemaDefinitionProperty<any, any> | undefined; }'.
            Property 'deleted' is incompatible with index signature.
              Type 'SchemaDefinitionProperty<boolean | undefined, ITest>' is not assignable to type 'SchemaDefinitionProperty<any, any> | undefined'.
                Type 'false' is not assignable to type 'SchemaDefinitionProperty<any, any> | undefined'.ts(2345)

Any ideas?

@Paso
Copy link
Author

Paso commented Feb 12, 2024

Didn't realize the types are separate from this repo. I have created an issue in DefinitelyTyped: DefinitelyTyped/DefinitelyTyped#68603

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant