sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

AllowNull doesn't change type?

Open uncleramsay opened this issue 4 years ago • 1 comments

Issue

Versions

  • sequelize: 6.6.4
  • sequelize-typescript: 2.1.0
  • typescript: 4.3.4

Issue type

  • [x] bug report
  • [ ] feature request

Actual behavior

I'm not sure if I'm doing something wrong, but I'm trying to allow null on certain columns and associations:

@ForeignKey(() => Arranger)
@AllowNull
@Column
arranger_id!: number;

@BelongsTo(() => Arranger, {foreignKey: {allowNull: true}})
arranger!: Arranger;

I would have expected the output types to take this into account, but it doesn't appear that they do. This is the generated d.ts file:

arranger_id: number;
arranger: Arranger;

Expected behavior

If I'm understanding everything correctly (I'm new to this package), I think the typings should be:

arranger_id: number | null;
arranger: Arranger | null;

Steps to reproduce

The snippets above should be enough to reproduce this I believe. Apologies if I've overlooked something in the docs.

uncleramsay avatar Jun 30 '21 19:06 uncleramsay

I'm not sure the decorators are that powerful. How would they produce arranger_id: number | null when the code block under "Actual behavior" explicitly has arranger_id!: number;? I agree that it would be nice for all types to flow from them rather than needing to be defined manually.

MBerka avatar Mar 25 '22 21:03 MBerka