Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js/Mongoose/MongoDb Typescript MapReduce - emit() and Array.sum() methods

I'm trying out a little side project with the MEAN stack and Typescript, and I've seem to run into an issue regarding Typescript not picking up on the typings for the emit() and Array.sum() methods.

Here's my code below...

let options: mongoose.ModelMapReduceOption<IInvoice, any, any> = {
    map: () => {
        emit(this.customer, this.total);
    },
    reduce: (key, values) => {
        return Array.sum(values);
    },
    out: { replace: "map_reduce_customers" },
    verbose: true
};

I am using the typings package on NPM, and have installed the typings for the mongodb as well as mongoose packages in my project. There are red squigglies under these two methods, yet the application works just fine when I run.

And yes, it is correctly transpiled to valid JavaScript. I would just like to know if there's a typing definition I'm missing for Typescript to pick up on these two methods?

like image 459
Richard Ueckermann Avatar asked Nov 09 '22 07:11

Richard Ueckermann


1 Answers

You can declare the emit function like this

declare function emit(k, v);

and then use non-arrow functions (to be able to use "this" inside the map function)

map: function map() {
    emit(this.customer, this.total);
}

Array.sum does not exist in JS as far as I know. If it's being provided by a library, you may want to install the typings for that library.

like image 191
Kizer Avatar answered Nov 15 '22 07:11

Kizer