Typescript: property data type does not always need to match the usage interface
See original GitHub issueIssue Description
Getters this.getDataValue
returns the type declared on the class property and not on the Column.type
.
Additional context
class Account extends Model<Account> {
@AllowNull
@Column({
type: DataType.DATEONLY, // SHOULD BE WHATEVER THIS IS
get(this: Account): moment.Moment | null {
const date = this.getDataValue('date_discharged'); // TYPESCRIPT SAYS THIS IS MOMENT | UNDEFINED
return getDate(date);
},
set(this: Account, val: moment.Moment | null) {
this.setDataValue('date_discharged', val == null ? null : val.format());
}
})
date_discharged?: moment.Moment; // BECAUSE OF THIS, I DO THIS TO USE MOMENT INSTEAD OF DATES.
}
Author of sequelize-typescript said the following
Hey @spearmootz thanks for reporting. Unfortunately this cannot be fixed within sequelize-typescript, but need to be solved on sequelize side. The actual issue is the type of getDataValue: https://github.com/sequelize/sequelize/blob/master/types/lib/model.d.ts#L2551. As your example makes it very clear, the internal type (the type stored in dataValues) doesn’t necessarily need to be the type of the property type. Can you open an issue in the sequelize repo, please?
Issue Template Checklist
Is this issue dialect-specific?
- No. This issue is relevant to Sequelize as a whole.
- Yes. This issue only applies to the following dialect(s): XXX, YYY, ZZZ
- I don’t know.
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 don’t know how to start, I would need guidance.
- No, I don’t have the time, although I believe I could do it if I had the time…
- No, I don’t have the time and I wouldn’t even know how to start.
I have no clue how to achieve the right type using typescript.
Although in the models tab, every time there is mention of this[k]
it is wrong, but rather it should be of the this[k].underlyingType
.
i am going to submit the ticket and simultaneously attempt to fix this.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:6 (5 by maintainers)
Thanks for the report. I edited your posts to add some formatting, please learn to do that in the future 😃
Any updates on this issue?