1

我正在尝试使用 .plugin 方法为扩展我的 Mongoose 模型的脚本创建 Typescript 头文件。Mongoose 头文件的当前签名:

export class Schema {
   // ...
   plugin(plugin: (schema: Schema, options?: Object) => void, 
                   options?: Object): Schema;
   // ...
}

Mongoose-lib 的一些实际代码:

/**
 * Registers a plugin for this schema.
 *
 * @param {Function} plugin callback
 * @param {Object} [opts]
 * @see plugins
 * @api public
 */

Schema.prototype.plugin = function (fn, opts) {
  fn(this, opts);
  return this;
};

然后是我自己的模型,扩展插件

import passportLocalMongoose = require('passport-local-mongoose')
// ...
var userSchema = new mongoose.Schema({
    email: String,
    password: String,
});
// ...
userSchema.plugin(passportLocalMongoose, {
    usernameField: "email",
    usernameLowerCase: true
});

来自护照本地猫鼬来源的片段:

module.exports = function(schema, options) {
   // ...   
   schema.methods.setPassword = function (password, cb) {
      // ...
   }

   schema.statics.authenticate = function() {
      // ...
   }
   // ...
}

我的主 app.js 出现问题

   // ...

   userSchema.authenticate()                // <<< Typescript error, undefined

   //   OR

   userSchemaInstance.setPassword(pass, cb) // <<< Typescript error, undefined

问题是 .authenticate 等是通过.methods.statics动态添加的...

我找不到在打字稿头文件中对此进行建模的方法。

我尝试了泛型和东西,但我不能(动态)将提供插件方法应用回原始模型。我还尝试plugin返回泛型T extends S & P(其中 S 从第一个参数扩展 Schema 和 P = 插件本身)。没运气 :-(

任何建议或示例如何解决这个问题?

4

1 回答 1

3

passport-local-mongoose.d.ts在文件中声明接口:

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 = _;
}

在你的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);
于 2015-08-12T22:59:07.020 回答