I have a class Pin that has the basic structure:
export default class Pin
extends Model<InferAttributes<Pin>, InferCreationAttributes<Pin>>
{
declare pinID: CreationOptional<IDType>
declare startingDate: Date
declare endingDate: Date | undefined
declare active: boolean
}
and the initialization:
Pin.init(
{
pinID: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true,
},
startingDate: { type: DataTypes.DATEONLY, allowNull: false },
endingDate: { type: DataTypes.DATEONLY, allowNull: true },
active: {
type: DataTypes.VIRTUAL(DataTypes.BOOLEAN, [
'startingDate',
'endingDate',
]),
set(value) {
throw new Error('Cannot set VIRTUAL column `active`')
},
get() {
return (
this.startingDate > new Date() &&
(!this.endingDate || this.endingDate < new Date())
)
}
}
}
)
But this approach with declare active: boolean gives me (understandably) the error:
Type 'PinData' is not assignable to type 'Omit<InferCreationAttributes<Pin, { omit: never; }>, NullishPropertiesOf<InferCreationAttributes<Pin, { omit: never; }>>>'.
Types of property 'active' are incompatible.
Type 'boolean | undefined' is not assignable to type 'boolean'.
Type 'undefined' is not assignable to type 'boolean'.
But I cannot declare active as NonAttribute, otherwise I could not add it as a virtual column (Object literal may only specify known properties, and 'active' does not exist in type 'ModelAttributes<Pin, Optional<InferAttributes<Pin, { omit: never; }>>>'.) Is there another utility-type provided by Sequelize to enable TS with virtual fields, or how can I fix this problem?