-
Notifications
You must be signed in to change notification settings - Fork 286
Type 'typeof Player' is not assignable to type 'typeof Model' #835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It's related to [email protected]. I have the same issue with both sequelize-typescript @1.1.0 & @2.0.0-beta.0 If I downgrade to [email protected] the issue is gone. It must be a change in the sequelize Model type which is not compatible with the decorators typing. Already reported in: #828, #826, #821 #813 Looks like using [email protected] solve the issue for now, which is the last version of sequelize with Model type compatible with sequelize-typescript. |
using [email protected] actually solves the issue, but creates another issue. It messes with the dependency injection in when using NestJS, the error is given as:
|
Hello @Austine105, did you found any solution for this issue ? I have the same problem with nest.js |
Yes I did. I used the factory pattern to configure sequelize as a provider and inject them in each module.
import DatabaseModule in your app.module file.
Add the provider to UserModule
Finally in UserService constructor
|
Hello, similar to the approach outlined above, has anyone been able to build something with private declarations and a constructor model? import {Column, DataType, Model, Table} from 'sequelize-typescript';
@Table({
timestamps: false,
freezeTableName: true,
tableName: 'author'
})
export class Author extends Model<Author> {
@Column({
type: DataType.NUMBER,
allowNull: false,
field: 'id',
primaryKey: true,
})
private _id: Number;
@Column({
type: DataType.STRING,
allowNull: false,
field: 'name',
})
private _name: string;
@Column({
type: DataType.STRING,
allowNull: false,
field: 'code',
})
private _code: string;
@Column({
type: DataType.STRING,
allowNull: false,
field: 'created_at',
})
private _createdAt: Date;
constructor(id: Number, name: string, code: string) {
super();
this._id = id;
this._name = name;
this._code = code;
this._createdAt = new Date();
}
public updateAuthor(name: string, code: string) {
this._name = name;
this._code = code;
}
get id(): Number {
return this._id;
}
get name(): string {
return this._name;
}
get code(): string {
return this._code;
}
} I would love to build a rich DDD style model, but I keep getting this error: The 'this' context of type 'typeof Author' is not assignable to method's 'this' of type '(new () => Author) & typeof Model'. Would love to see if someone has tried similar approaches. |
Same here, @RobinBuschmann any idea what's going on? |
I have the same isssue with [email protected] and [email protected] |
I have the same problem with
and
resolved with downgrade to
|
I've created a simple reproduction in TypeScript Playground (Here). I'm not that good at TypeScript but when it's in it's simplest form it's easier to play around with it until the error disappears and we can PR/patch it. After playing with it a little I asked in https://gitter.im/Microsoft/TypeScript and a guy named
Here's the Playground example that fixes it, which also seems to work in other places like Includable which also has the same problem. It's a bit verbose but maybe it's the necessary evil to achieve what we want. TL;DR: export type TypeOfModel<TCreationAttributes, TModelAttributes> = new (values?: TCreationAttributes, options?: any) => Model<TModelAttributes, TCreationAttributes>;
export type Includeable<TCreationAttributes, TModelAttributes> = TypeOfModel<TModelAttributes, TCreationAttributes> | Association | IncludeOptions | { all: true, nested?: true } | string;
export declare type ModelClassGetter<TCreationAttributes, TModelAttributes> = (returns?: void) => TypeOfModel<TCreationAttributes, TModelAttributes>;
export declare function HasOne<TCreationAttributes, TModelAttributes>(associatedClassGetter: ModelClassGetter<TCreationAttributes, TModelAttributes>, foreignKey?: string): Function;
export declare function HasOne<TCreationAttributes, TModelAttributes>(associatedClassGetter: ModelClassGetter<TCreationAttributes, TModelAttributes>, options?: HasOneOptions): Function;
etc |
Thanks your proposal can solve the issue |
You can use -export class Team extends Model<Team> {
+export class Team extends Model { Thanks to @lukashroch for updated docs: https://github.com/lukashroch/sequelize-typescript/tree/sequelize6-docs#v6-model-definition-less-strict |
Hi guys, I provide a workaround to allow strict model attribute type-checks. Works for:
import {
AutoIncrement,
Column,
HasMany,
Model,
ModelCtor,
PrimaryKey,
Sequelize,
Table,
} from 'sequelize-typescript';
type ModelAttributes<T> = Omit<T, keyof Model>;
type CreationAttributes<T> = {
[key in keyof ModelAttributes<T>]?: ModelAttributes<T>[key];
};
// 1. Created a strict typed model class
export class TypedModel<T> extends Model<ModelAttributes<T>, CreationAttributes<T>> {
// provides a less strict type Model
static get Model(): ModelCtor {
return this as any;
}
}
// 2. Extends TypedModel instead of Model
@Table
class User extends TypedModel<User> {
@AutoIncrement
@PrimaryKey
@Column
userId: number;
@Column
userName: string;
}
@Table
class Team extends TypedModel<Team> {
@AutoIncrement
@PrimaryKey
@Column
teamId: number;
// 3. Use less strict type Model to make type check successfully
@HasMany(() => User.Model)
members: User[];
}
const sequelize = new Sequelize({
// ... other initialize options
// 4. Just use less strict type Model wherever you needs
models: [User.Model, Team.Model],
});
User.findAll({
// 5. now you got a type-safe where!
where: {
userName: 'techirdliu',
},
}); |
@KapitanOczywisty solution also works with:
So i think we can work free with the latest versions |
I'm not sure if the above fixes the problem with Includeables: PaymentCard.findOne({
transaction,
include: [BillingAddress],
where: { active: true },
}), Gives:
And I think it's the initial problem I mentioned in my original PR. That Sequelize has it's own Model types that we'd need to override somehow. It works if you remove the Attrs and CreateAttr types: @Table
export class PaymentCard extends Model {
@HasOne(() => BillingAddress)
public billingAddress: BillingAddress;
}
@Table
export class BillingAddress extends Model {...} |
There is a PR in the |
Hello, how ab out this issue ? Because i'm facing exactly the same. I have a generic argument to a function that takes a model constructor like 'Runner' but it can't build because typeof Runner is n ot assignable to ModelCtor
it's very curious because if this type doesn't help to assign a Model to a typed property how can we achieve this ? is this a bug or a misunderstanding about how to use sequelize types ? here is my code:
thanks for help |
Have you looked at the solution above |
@therougeb Provided code is missing crucial parts, but I'm guessing you are mixing |
For people wondering in which release this issue was resolved in sequelize: v6.6.1 |
I have been rewriting an old pure js code to ts and have got this error. The issue was that I had used the |
@RobMico you're saver. thank you! |
Versions
I'm submitting a ...
[X] bug report
[ ] feature request
Actual behavior:
I have added 2 class files, Player.ts and Team.ts as per the example. In each of the references:
and
The Team and the Player following the => on the attribute is underlines in red in VS code with the error:
Expected behavior:
No Errors
Steps to reproduce:
Added Player.ts with the sample code provided in the readme.md, and Team.ts in the same way
Related code:
Player.ts
Team.ts
The text was updated successfully, but these errors were encountered: