Dynamoose: Model/Schema attributes type issue

Created on 2 Jul 2019  路  3Comments  路  Source: dynamoose/dynamoose

Summary:

I am using Typescript and I have defined a model and a schema (see code sample below). The problem is that the type checking can't see the attributes defined in the schema.

e.g.

const user = new User({ email: '[email protected]', name: 'Mark' });
// user.email -- it doesn't know about the email attribute

Code sample:

Schema

import dynamoose from './dynamoose';
const Schema = dynamoose.Schema;

const schema = new Schema({
  email: {
    type: String,
    hashKey: true
  },
  name: { type: String }
});

Model

const User = dynamoose.model('users', schema, { create: false });

General

const user = new User({ email: '[email protected]', name: 'Mark' });
// user.email -- it doesn't know about the email attribute

Current output and behavior:

when I access user.email, in vscode, it displays Property 'email' does not exist on type 'Model'

Expected output and behavior:

I expect the types to be included.

Environment:

Operating System: Mac OSX
Operating System Version: Mojave
Node.js version (node -v): 8.13
NPM version: (npm -v): 6.4
Dynamoose version: 1.10.0
Dynamoose Plugins: none

-

Dynamoose Plugins:

  • [ ] Yes, I believe that one or multiple 3rd party Dynamoose plugins are effecting this issue
  • [ ] No, I believe this issue is independent of any 3rd party Dynamoose plugins I'm using
  • [ ] Unknown, I'm unsure if Dynamoose plugins are effecting this issue
  • [x] I am not using any Dynamoose plugins

Other information (if applicable):

Type (select 1):

  • [ ] Bug report
  • [ ] Feature suggestion
  • [x] Question
  • [ ] Other suggestion
  • [ ] Something not listed here

Other:

  • [x] I have read through the Dynamoose documentation before posting this issue
  • [x] I have searched through the GitHub issues (including closed issues) and pull requests to ensure this issue has not already been raised before
  • [x] I have searched the internet and Stack Overflow to ensure this issue hasn't been raised or answered before
  • [x] I have tested the code provided and am confident it doesn't work as intended
  • [x] I have ensured that all of my plugins that I'm using with Dynamoose are listed above
  • [x] I have filled out all fields above
  • [x] I am running the latest version of Dynamoose

Most helpful comment

yeah. the types need a lot of love. lemme take a pass later today at just fixing the typings. the use of generics isn鈥檛 quite right and it鈥檚 missing a lot of methods

All 3 comments

@hweeks I was having the same issue as well and had to cast my models to custom interfaces I defined for them. It doesn't look like there's an easy solution to that problem with current Dynamoose typings?

yeah. the types need a lot of love. lemme take a pass later today at just fixing the typings. the use of generics isn鈥檛 quite right and it鈥檚 missing a lot of methods

I see this is quite old but for anyone looking for this, the model use generics and you need to inform the model:

export interface User extends Document {
    name: string;
    email: string;
}

export const UserSchema = dynamoose.schema({
    email: {
        type: String,
        hashKey: true,
    },
    name: {
        required: true,
        type: String,
    },
});
const UserModel: ModelType<User> = dynamoose.model<User>('user', UserSchema);
export default UserModel;
Was this page helpful?
0 / 5 - 0 ratings

Related issues

dolsem picture dolsem  路  10Comments

fishcharlie picture fishcharlie  路  7Comments

jdcrecur picture jdcrecur  路  9Comments

chasevida picture chasevida  路  3Comments

MickL picture MickL  路  7Comments