Hi,
I am experiencing some weird stuff.
I have a multi module TS project with path-mapping in my tsconfig.json.
Some of my Models are in one module, some (like User) are in another module.
When I try to declare something like:
@ForeignKey(() => User)
@Column
createdByUserId: number
@BelongsTo(() => User)
createdByUser: User
I get something like:
error TS2345: Argument of type '() => typeof User' is not assignable to parameter of type 'ModelClassGetter'.
Type 'typeof User' is not assignable to type 'typeof Model'.
Types of property 'schema' are incompatible.
Type '<T extends Model<T>>(this: new () => T, schema: string, options?: sequelize.SchemaOptions | undef...' is not assignable to type '<T extends Model<T>>(this: new () => T, schema: string, options?: sequelize.SchemaOptions | undef...'. Two different types with this name exist, but they are unrelated.
The 'this' types of each signature are incompatible.
Type 'new () => T' is not assignable to type 'new () => Model<{}>'.
Type 'T' is not assignable to type 'Model<{}>'.
Type 'Model<T>' is not assignable to type 'Model<{}>'.
Types of property '$add' are incompatible.
Type '<R extends Model<R>>(propertyKey: string, instances: R | R[], options?: IAssociationActionOptions...' is not assignable to type '<R extends Model<R>>(propertyKey: string, instances: R | R[], options?: IAssociationActionOptions...'. Two different types with this name exist, but they are unrelated.
Types of parameters 'instances' and 'instances' are incompatible.
Type 'R | R[]' is not assignable to type 'Model<R> | Model<R>[]'.
Type 'R' is not assignable to type 'Model<R> | Model<R>[]'.
Type 'Model<R>' is not assignable to type 'Model<R> | Model<R>[]'.
Type 'Model<R>' is not assignable to type 'Model<R>[]'.
Type 'R' is not assignable to type 'Model<R>[]'.
Type 'Model<R>' is not assignable to type 'Model<R>[]'.
Property 'length' is missing in type 'Model<R>'.
And the compiler takes forever to do its job.
All works fine if I keep them in the same module, so my guess this might be a TS issue.
Any tips on why this might happen?
Hey @dragosbulugean, thanks for reporting. Did you install sequelize-typescript in both - in your current project and in your module?
Yes, it's installed, otherwise I couldn't define models :)
I am somehow under the impression this is a TS issue, that it doesn't pass decorator information from module to module, because if I compile the module the d.ts's generated don't contain decorators, so it might use the same mechanism.
When you saying „module“, do you mean another npm package in which additional models are kept? In this case you should have sequelize-typescript only installed on your main project... not additionally in these packages. So that sequelize typescripts doesn‘t get installed more than once in your project. If this is not the case, can you provide an example repo which reproduces the issue?
OMG this was the problem. Having multiple sequelize-typescript installs. Thank you!
But now I have a different one, in that it can't do inference even on simple Date.
throw new Error("Specified type of property '" + propertyName + "'\n cannot be automatically resolved to a sequelize data type. Please\n define the data type manually");
Good to here :)
Regarding your other issue: Did you set "experimentalDecorators": true,"emitDecoratorMetadata": true in your modules tsconfig.json?
Yes, of course. Each module has its own tsconfig.json and both flags are set in both of them.
Does your compiled files contain something like:
__decorate([
index_1.Column(index_1.DataType.INTEGER),
__metadata("design:type", Object), // <--
__metadata("design:paramtypes", [Object]) // <--
], Book.prototype, "year", null);
I am good now :)
The last problem was due to the fact that the second module had "typings" in package.json pointing at d.ts in the build folder.
So frustrating...
Thank you for support,
Awesome project BTW, I will contribute if needed.
Dragos
In case someone experiences this issue, do NOT forget to declare the property type if you don't set it in the @Column signature.
In a nutshell, this will raise an error (asking to specify column type):
@Column
@CreatedAt
public CreatedAt;
While this won't:
@Column
@CreatedAt
public CreatedAt: Date;
Most helpful comment
Good to here :)
Regarding your other issue: Did you set
"experimentalDecorators": true,"emitDecoratorMetadata": truein your modules tsconfig.json?