title: Cannot access mongoose Schema context in Typescript
body: <p>I'm trying to apply <code>.pre</code>, <code>.method</code>, and <code>.static</code> functions to my Mongoose Schemas. </p>
  
  <p>I have the following code, but my <code>this</code> is either in the wrong context or I'm misunderstanding Schemas.</p>
  
  <pre><code>export interface IUser extends mongoose.Document {
      email: string;
      password: string;
      firstName: string;
      lastName: string;
      comparePassword(password: string, callback: Function): void;
  }
  
  class UserSchema {
      static get schema(): mongoose.Schema {
          let userSchema = new mongoose.Schema({
              email: {
                  type: String,
                  unique: true,
                  lowercase: true
              },
              firstName: {
                  type: String,
                  required: true
              },
              lastName: {
                  type: String,
                  required: true
              },
              password: {
                  type: String,
                  select: false
              }
          });
  
          userSchema.pre('save', this.saveHandler);        
          userSchema.method('comparePassword', this.comparePassword);
  
          return userSchema;
      }
  
      static saveHandler(next) {
          let user = this.schema;
          if(!user.isModified('password')) {
                return next();
           }
            bcrypt.genSalt(10, (err, salt) =&gt; {
                bcrypt.hash(user.password, salt, (err, hash) =&gt; {
                    user.password = hash;
                    next();
                });
            });
      }
  
      static comparePassword(password: string, callback: Function) {
          let user = this.schema;
          bcrypt.compare(password, user.password, (err, isMatch) =&gt; {
              callback(err, isMatch);
          });
      }
  }
  
  export const UserModel = mongoose.model&lt;IUser&gt;('User', UserSchema.schema);
  </code></pre>
  
  <p>Typescript is throwing errors that <code>Property 'isModified' does not exist on type 'Schema'.</code></p>
  
  <p>However, in javascript:</p>
  
  <pre><code>var schema = new Schema({
      email: { type: String, unique: true, lowercase: true },
      password: { type: String, select: false },
      firstName: String,
      lastName: String,
  });
  
  schema.pre('save', function(next) {
      var user = this;
      if(!user.isModified('password')) {
          return next();
      }
      bcrypt.genSalt(10, function(err, salt) {
          bcrypt.hash(user.password, salt, function(err, hash) {
              user.password = hash;
              next();
          });
      });
  });
  </code></pre>
  
  <p>does have the correct <code>this</code> context. How would I properly structure my Typescript code to have the same effect?</p>