sequelize icon indicating copy to clipboard operation
sequelize copied to clipboard

Sequelize v6.x.x model definition by extending Model class with TypeScript v5.x.x issue

Open oglisa-dev opened this issue 2 years ago • 1 comments

Issue Creation Checklist

  • [x] I understand that my issue will be automatically closed if I don't fill in the requested information
  • [x] I have read the contribution guidelines

Bug Description

Sequelize v6 model definition by extending Model class using TypeScript v5.x.x results in tsc compilation errors:

  1. Method 'set' in class 'X' is not assignable to the same method in base 'Model'
  2. Method 'setAttributes' in class 'X' is not assignable to the same method in base 'Model'

where class X is model being defined using inheritance.

Same tsc compilation errors occur when using the sequelize-typescript v2.x.x and TypeScript v5.x.x..

Warnings are raised when using Sequelize v7.x.x and TypeScript v5.x.x.

Reproducible Example

Here is the link to the SSCCE for this issue: https://github.com/oglisa-dev/sequelize-typescript-v5-issue-sscce

What do you expect to happen?

Arbitrary model X should be defined by extending Model class without any tsc compilation errors when using Sequelize v6.x.x and TypeScript v5.x.x.

Arbitrary model X should be defined by extending Model class using sequelize-typescript without any tsc compilation errors when using sequelize-typescript v2.x.x and TypeScript v5.x.x.

Arbitrary model X should be defined by extending Model class without any warnings when using Sequelize v7.x.x and TypeScript v5.x.x.

What is actually happening?

// using sequelize v6
import { DataTypes, Model, Sequelize } from "sequelize";

// ts errors:
// Method 'set' in class 'User' is not assignable to the same method in base 'Model'
// Method 'setAttributes' in class 'User' is not assignable to the same method in base 'Model'
export class User extends Model {
  declare firstname: string;
  declare lastname: string;
}
// using sequelize-typescript v2
import { Table, Column, Model } from 'sequelize-typescript';

// ts errors
// Method 'set' in class 'Person' is not assignable to the same method in base 'Model'
// Method 'setAttributes' in class 'Person' is not assignable to the same method in base 'Model'
@Table
export class Person extends Model {
  @Column
  firstname: string;

  @Column
  lastname: Date;
}
// using sequelize v7
import {CreationOptional, DataTypes, InferAttributes, InferCreationAttributes, Model} from '@sequelize/core';
import { Attribute, PrimaryKey, AutoIncrement, NotNull } from '@sequelize/core/decorators-legacy';

// warnings (not errors)
// Method 'set' in class 'User' is not assignable to the same method in base 'Model'
// Method 'setAttributes' in class 'User' is not assignable to the same method in base 'Model'
export class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
  @Attribute(DataTypes.INTEGER)
  @PrimaryKey
  @AutoIncrement
  declare id: CreationOptional<number>;

  @Attribute(DataTypes.STRING)
  @NotNull
  declare firstName: string;

  @Attribute(DataTypes.STRING)
  declare lastName: string | null;
}

Environment

  • Sequelize version: v6.33.0
  • Node.js version: v20.7.0
  • TypeScript version: v5.2.2

Would you be willing to resolve this issue by submitting a Pull Request?

  • [ ] Yes, I have the time and I know how to start.
  • [ ] Yes, I have the time but I will need guidance.
  • [ ] No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • [x] No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

oglisa-dev avatar Oct 02 '23 22:10 oglisa-dev

Does anybody have a solution or workaround for this in the mean time???

hackerunet avatar Jan 16 '24 23:01 hackerunet