sequelize / sequelize-typescript

Decorators and some other features for sequelize

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TS error when tried to change createdAt, updatedAt columns type to something else

apudiu opened this issue · comments

Issue

I want to override createdAt columns type from Date to DateTime(from Luxon). When I went to declare that getting TS error.

TS2612: Property createdAt will overwrite the base property in Model<any, any>. If this is intentional, add an initializer. Otherwise, add a declare modifier or remove the redundant declaration.

Versions

  • sequelize: 6.35.2,
  • sequelize-typescript: 2.1.6
  • typescript: 5.3.3

Issue type

  • [x ] bug report
  • feature request

Actual behavior

I'm working on Nest.js project & followed their docs

Expected behavior

Want to be able to override the type of createdAt, updatedAt & deletedAt columns data type

Steps to reproduce

On a fresh Nest.js project try to override/ change data type from Date to DateTime for those columns

Related code

// the model
export class User extends Model {
  @Column
  name: string;

  // ....

  @Column
  dob: DateTime;

  @Column
  refreshToken: string | null;

  @CreatedAt
  createdAt: DateTime;
// ^^^^^^^^ TS Err

  @UpdatedAt
  updatedAt: DateTime; 
// ^^^^^^^^ TS Err
}

// tsconfig
"compilerOptions": {
    "module": "CommonJS",
    "declaration": false,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2022",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": true,
    "noImplicitAny": false,
    "strictBindCallApply": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
  }

// sequelize config
SequelizeModule.forRootAsync({
      inject: [ConfigService],
      useFactory: (cs: ConfigService<AppConfig>) => {
        return { // this is the config
          dialect: 'mariadb',
          host: cs.get('db.host', { infer: true }),
          port: cs.get('db.port', { infer: true }),
          username: cs.get('db.user', { infer: true }),
          password: cs.get('db.pass', { infer: true }),
          database: cs.get('db.name', { infer: true }),
          models: [],
          autoLoadModels: true,
          synchronize: true,
          retryAttempts: 3,
          retryDelay: 500,
        };
      },
    })

Upgrade from ES2021 to ES2022 causes this issue