scottwrobinson / camo

A class-based ES6 ODM for Mongo-like databases.
556 stars 80 forks source link

Typescript definitions question #87

Open zinas opened 7 years ago

zinas commented 7 years ago

I am trying to define a very simple class, like this:

import { Document, DocumentSchema, SchemaTypeExtended } from 'camo';

export interface UserSchema extends DocumentSchema {
    email: string;
    password: string;
}

export class User extends Document<UserSchema> {
    public email: SchemaTypeExtended = String;
    public password: SchemaTypeExtended = String;
}

Now, lets assume, I am trying to create a new user:

let newUser = User.create({ email: 'A', password: 'B' });

console.log(newUser.email) // prints 'A', but typescript throws error

newUser.save().then(user => {
  console.log(user.email); // prints 'A' and typescript doesn't complain
});

Any idea how I can fix the error?

HenryNguyen5 commented 7 years ago

I have the same problem as above

guysenpai commented 7 years ago

Use

let newUser = create<UserSchema>(...)
guysenpai commented 7 years ago

I mean

let newUser = User.create<UserSchema>(...);