typescript - Header files for Mongoose' 'plugin' method (extending via .methods and .statics) -
i'm trying create typescript header files script extends mongoose-model using .plugin method. current signature mongoose header-files:
export class schema { // ... plugin(plugin: (schema: schema, options?: object) => void, options?: object): schema; // ... }
some actual code mongoose-lib:
/** * registers plugin schema. * * @param {function} plugin callback * @param {object} [opts] * @see plugins * @api public */ schema.prototype.plugin = function (fn, opts) { fn(this, opts); return this; };
then own model, extending plugin;
import passportlocalmongoose = require('passport-local-mongoose') // ... var userschema = new mongoose.schema({ email: string, password: string, }); // ... userschema.plugin(passportlocalmongoose, { usernamefield: "email", usernamelowercase: true });
snippet passport-local-mongoose source:
module.exports = function(schema, options) { // ... schema.methods.setpassword = function (password, cb) { // ... } schema.statics.authenticate = function() { // ... } // ... }
problem occurs in main app.js
// ... userschema.authenticate() // <<< typescript error, undefined // or userschemainstance.setpassword(pass, cb) // <<< typescript error, undefined
the problem .authenticate etc. dynamically added through .methods , .statics ...
i can't find way model in typescript header files.
i tried generics , stuff, can't (dynamic) apply provide plugin-methods original model. tried plugin
returning generic t extends s & p
(where s extends schema first argument , p = plugin itself). no luck :-(
any suggestions or examples how solve this?
declare interfaces in passport-local-mongoose.d.ts
file:
declare module 'mongoose' { // methods export interface passportlocaldocument extends document { setpassword(pass: string, cb: (err: any) => void); } // statics export interface passportlocalmodel<t extends passportlocaldocument> extends model<t> { authenticate(username: string, password: string, cb: (err: any) => void); } // plugin options export interface passportlocaloptions { usernamefield?: string; usernamelowercase?: boolean; } export interface passportlocalschema extends schema { plugin( plugin: (schema: passportlocalschema, options?: passportlocaloptions) => void, options?: passportlocaloptions): schema; } export function model<t extends passportlocaldocument>( name: string, schema?: passportlocalschema, collection?: string, skipinit?: boolean): passportlocalmodel<t>; } declare module 'passport-local-mongoose' { import mongoose = require('mongoose'); var _: (schema: mongoose.schema, options?: object) => void; export = _; }
use in app.ts
:
import mongoose = require('mongoose'); import passportlocalmongoose = require('passport-local-mongoose'); interface userdocument extends mongoose.passportlocaldocument { email: string, password: string; } var userschema = <mongoose.passportlocalschema>new mongoose.schema({ email: string, password: string }); userschema.plugin(passportlocalmongoose, { usernamefield: 'email', usernamelowercase: true }); var user = mongoose.model<userdocument>('user', userschema); user.authenticate(username, pass, cb); var user = new user(); user.setpassword(newpass, cb);
Comments
Post a Comment