2
votes

In my mongoose schema I use the following library like this: https://github.com/VassilisPallas/mongoose-fuzzy-searching

UserSchema.plugin(mongoose_fuzzy_searching, { fields: ['name'] });

And then in the service I use it like this:

export function fuzzySearchUser(name: string): Query<Array<IUser>>{
    return User.fuzzySearch(name)
}

Now in the return line above get the following Typescript error:

Property 'fuzzySearch' does not exist on type 'Model<IUser, {}>'.ts(2339)

It doesn't help if I add it as a property with type function to the IUser interface and I cannot add it to the schema either.

(interface IUser extends mongoose.Document)

1

1 Answers

3
votes

You need to create a typing file as this lib does not provide typings. Try this out.

typings/mongoose-fuzzy-search

declare module 'mongoose-fuzzy-search' {
  import { Document, DocumentQuery, Model, Schema } from 'mongoose'

  export interface MongooseFuzzyOptions<T> {
    fields: (T extends Object ? keyof T : string)[]
  }

  export interface MongooseFuzzyModel<T extends Document, QueryHelpers = {}>
    extends Model<T, QueryHelpers> {
    fuzzySearch(
      search: String,
      callBack?: (err: any, data: Model<T, QueryHelpers>[]) => void
    ): DocumentQuery<T[], T, QueryHelpers>
  }

  function fuzzyPlugin<T>(schema: Schema<T>, options: MongooseFuzzyOptions<T>): void

  export default fuzzyPlugin
}

Then declare your model using MogooseFuzzyModel:

export const User = mongoose.model<UserModel>('User', UserSchema) as MongooseFuzzyModel<UserModel>