It seems there are still problems when defining models with stricter typing syntax on attributes, as already reported here https://github.com/RobinBuschmann/sequelize-typescript/issues/844.
The following code does not compile:
import { Optional } from 'sequelize';
import { Sequelize, Model } from "sequelize-typescript";
interface PersonAttributes {
id: number;
name: string;
country?: string;
}
interface PersonCreationAttributes extends Optional<PersonAttributes, 'country'> {}
class Person extends Model<PersonAttributes, PersonCreationAttributes> implements PersonAttributes {
id!: number;
name!: string;
country?: string;
}
(async () => {
const sequelize = new Sequelize({
dialect: 'mysql',
host: 'localhost',
port: 3306,
database: 'testdb',
username: 'admin',
password: 'admin',
logQueryParameters: true,
logging: true,
});
await sequelize.authenticate();
await sequelize.addModels([Person]); // <-- compiler complains
await sequelize.close();
})();

See also:
Hey @spinlud, thanks for reporting.
This doesn't seem to be a sequelize-typescript (only) issue. Similar code also throws a compiler error with pure sequelize:
import {Model} from 'sequelize';
class Test extends Model<{test: string}> {}
class Test2 extends Model {}
Test2.findAll({
include: [Test], // fails here
});
```
TS2322: Type 'typeof Test' is not assignable to type 'Includeable'. 聽聽
Type 'typeof Test' is not assignable to type 'typeof Model'. 聽聽聽聽
Types of construct signatures are incompatible. 聽聽聽聽聽聽
Type 'new (values?: { test: string; } | undefined, options?: BuildOptions | undefined) => Test' is not assignable to type 'new
Types of parameters 'values' and 'values' are incompatible. 聽聽聽聽聽聽聽聽聽聽
Type 'TCreationAttributes | undefined' is not assignable to type '{ test: string; } | undefined'. 聽聽聽聽聽聽聽聽聽聽聽聽
Type 'TCreationAttributes' is not assignable to type '{ test: string; } | undefined'. 聽聽聽聽聽聽聽聽聽聽聽聽聽聽
Type '{}' is not assignable to type '{ test: string; }'. 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
Type 'TCreationAttributes' is not assignable to type '{ test: string; }'. 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
Property 'test' is missing in type '{}' but required in type '{ test: string; }'.
Can anyone confirm?
The source of the problem seems to be the default value of generic TCreationAttributes in the definition of `Model`(sequelize).
See following example:
```ts
class Model<TModelAttributes = any, TCreationAttributes = TModelAttributes> {
constructor(values?: TCreationAttributes) {}
}
class Test extends Model<{test: string}> {}
const addModel = (model: typeof Model) => {};
addModel(Test);
(see in typescript playground)
If we change TCreationAttributes = TModelAttributes to TCreationAttributes = any the error disappears. Which is strange, since TCreationAttributes should be inferred to any anyway.
I'm considering to just use type ModelCtor<M extends Model = any> = (new (...args: any[]) => M) instead of export declare type ModelCtor<M extends Model = any> = (new () => M) & ModelType;(so omitting ModelType, which is an alias for typeof Model) in sequelize-typescriot. It fixes the problem and should give "enough" type safety, I think. But the IncludeOptions und the Includable type of sequelize need to be fixed as well: https://github.com/sequelize/sequelize/blob/main/types/lib/model.d.ts#L394
@RobinBuschmann I can confirm I'm seeing the same error with just sequelize.
Your playground example is very interesting 馃
Found a related issue on the sequelize side:
https://github.com/sequelize/sequelize/issues/12842
It seems like, for now the only workaround is to use //@ts-ignore where this happens, right?
@AlexanderProd Yes, I've been ignoring the errors, didn't run into any problems yet.
But... I guess PR #900 would fix this.
-edit-
Looks like 2.1.0 was released a couple of hours ago, I will check it out tomorrow.
@RoelVB
The problem still happens in 2.1.0 for me.
@AlexanderProd I just tested [email protected] with [email protected] and the problem described in this issue disappeared.
I only noticed the typings issue is still there with includes. This is waiting for https://github.com/sequelize/sequelize/pull/13010
Issue should be fixed in sequelize 6.6.1, please verify. @Divlo
Apologies for the delay.
Yes I can confirm that with the new versions the problem is solved.
I've also updated sequelize-typescript-generator to leverage the strict mode as well.