yakkisyou
yakkisyou

Reputation: 520

Sequelize, Typescript - typescript make an error to model's getter method

Let's say, I have two models User and Post, and they are associated as one to many relationship:

const User = sequelize.define('user', {
    ...
});

const Post = sequelize.define('post', {
    ...
});

User.hasMany(Post);

Then if I get some posts from a user using getPosts method,

const user = await User.findOne({...});
const posts = await user.getPosts();
                         ^^^^^^^^

Typescript makes an error:

error TS2339: Property 'getPosts' does not exist on type '{}'.

What should I do for it? Any suggestions will be appreciated.

Upvotes: 0

Views: 1282

Answers (1)

Matt McCutchen
Matt McCutchen

Reputation: 30919

From what I read, the TypeScript typings for Sequelize are incomplete. You can specify the attributes of your objects when you call sequelize.define as shown here, but this won't declare a getPosts method AFAIK. The sequelize-typescript package appears to offer an alternative way to get related objects, though I haven't tried any of this myself.

Upvotes: 1

Related Questions