Sequelize V5->V6 upgrade: Typescript Error Property 'x' does not exist on type 'y'

Viewed 1898

I will upgrade from sequelize 5.x.x to 6.x.x. In sequelize v5 everything works well but when i upgrade, all properties which are generated via associations get a typescript error when i want to use objects from the include. I tried also the examples from https://sequelize.org/master/manual/typescript.html without success.

I hope anyone can help me!!

Model: Participant.js

import { Model, DataTypes } from 'sequelize';
import { STATUS } from '@bb/shared-core/dist/constants/participant';

import sequelize from '@bb/db';
import User from '@bb/components/erp/v1/modules/users/models/User';
import OrderItem from '@bb/components/ecommerce/v2/modules/orders/models/OrderItem';
import Contact from '@bb/components/erp/v1/modules/contacts/models/Contact';

import { DeserializedModel } from '../interfaces/Participant';

export class Participant extends Model implements DeserializedModel {
  id!: number;
  fkUser: number;
  fkOrderItem: number;
  fkContact: number;
  adminNotes: string;
  trainerNotes: string;
  attendance: string;
  status: string;
  readonly createdAt: Date;
  readonly updatedAt: Date;
}

Participant.init({
  fkUser: {
    type: DataTypes.NUMBER,
    field: 'fk_user'
  },
  fkOrderItem: {
    type: DataTypes.NUMBER,
    field: 'fk_order_item'
  },
  fkContact: {
    type: DataTypes.NUMBER,
    field: 'fk_contact'
  },
  trainerNotes: {
    type: DataTypes.STRING,
    field: 'trainer_notes'
  },
  adminNotes: {
    type: DataTypes.STRING,
    field: 'admin_notes'
  },
  attendance: {
    type: DataTypes.ENUM('confirmed', 'unconfirmed'),
    field: 'attendance'
  },
  status: {
    type: DataTypes.ENUM(STATUS.REBOOKED, STATUS.CANCELLED, STATUS.PARTICIPATING),
    field: 'status',
    defaultValue: 'participating'
  }
}, {
  sequelize,
  modelName: 'participant'
});

Participant.belongsTo(User, {
  as: 'user',
  foreignKey: 'fk_user',
  targetKey: 'id'
});

Participant.belongsTo(OrderItem, {
  as: 'orderItem',
  foreignKey: 'fk_order_item',
  targetKey: 'id'
});

OrderItem.hasMany(Participant, {
  as: 'participants',
  foreignKey: 'fk_order_item',
  sourceKey: 'id'
});

Participant.belongsTo(Contact, {
  as: 'contact',
  foreignKey: 'fk_contact',
  targetKey: 'id'
});

if (process.env.NODE_ENV === 'test') {
  Participant.sync();
}

export default Participant;

And here is the function which used the model: ParticipantService.js

  private async getEventIdFromParticipant(participantId: number): Promise<number|undefined> {
    const participant = await Participant.findOne({
      where: {
        id: participantId,
      },
      include: [
        {
          model: OrderItem,
          as: 'orderItem',
        }
      ]
    });

    return participant?.orderItem?.fkEvent;
  }

The error is in ParticipantService.js in the return statement.

Property 'orderItem' does not exist on type 'Participant'
1 Answers

You need to define it in the class too:

import { Model, DataTypes, BelongsTo } from 'sequelize';

export class Participant extends Model implements DeserializedModel {
  id!: number;
  fkUser: number;
  fkOrderItem: number;
  fkContact: number;
  adminNotes: string;
  trainerNotes: string;
  attendance: string;
  status: string;
  public readonly orderItem?: OrderItem;
  public static associations: {
    orderItem: BelongsTo<Participant, OrderItem>;
  };
  readonly createdAt: Date;
  readonly updatedAt: Date;
}
Related