apache / couchdb-nano

Nano: The official Apache CouchDB library for Node.js
https://www.npmjs.com/package/nano
Apache License 2.0
651 stars 165 forks source link

[Types]: Add string as input type for View.map property #219

Closed BigsonLvrocha closed 2 years ago

BigsonLvrocha commented 4 years ago

Expected Behavior

I should be able to define functions passing a string to views field

await handle.insert({
      _id: '_design/des',
      view: {
        view1: `
          function (doc) {
            if (doc.ClassName === 'viewClass') {
              emit([doc.key1, doc.key2])
            }
          }
        `
      }
    })

Current Behavior

It throws the error

Argument of type '{ _id: string; view: { view1: string; }; }' is not assignable to parameter of type 'ViewDocument<ActionCouchDoc> | (ActionCouchDoc & MaybeDocument)'.
  Object literal may only specify known properties, and 'view' does not exist in type 'ViewDocument<ActionCouchDoc> | (ActionCouchDoc & MaybeDocument)

Possible Solution

change

type DocumentInfer<D> = (doc: D & Document) => void
  interface View<D> {
    map?:DocumentInfer<D>;
    reduce?: string | DocumentInfer<D>

  }

  interface ViewDocument<D> extends IdentifiedDocument {
    views: {
      [name: string]: View<D>
    };
  }

to

type DocumentInfer<D> = (doc: D & Document) => void
  interface View<D> {
    map?:string |DocumentInfer<D>;
    reduce?: string | DocumentInfer<D>

  }

  interface ViewDocument<D> extends IdentifiedDocument {
    views: {
      [name: string]: View<D>
    };
  }

Context

I've been having to cast the string to any for it to stop complaining and it still woks

await handle.insert({
      _id: '_design/des',
      view: {
        view1: `
          function (doc) {
            if (doc.ClassName === 'viewClass') {
              emit([doc.key1, doc.key2])
            }
          }
        `
      }
    })

Your Environment